Skip to content

Commit 48c016f

Browse files
refactor: simplify mbedlts compilation script (#91)
* rework compile Co-authored-by: Aleksandr Kolosov <117081005+akolosov-n@users.noreply.github.com>
1 parent 0d15b12 commit 48c016f

File tree

22 files changed

+47
-39
lines changed

22 files changed

+47
-39
lines changed

.github/workflows/compile-mbedtls.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
VENV_DIR=$(mktemp -d)
5252
python3 -m venv ${VENV_DIR}
5353
source ${VENV_DIR}/bin/activate
54-
LDFLAGS='-arch x86_64 -arch arm64' CFLAGS='-O2 -arch x86_64 -arch arm64' DLEXT=dylib OSARCH=darwin ./compileMbedtls.sh
54+
DLEXT=dylib OSARCH=darwin CMAKE_EXTRA='-DCMAKE_OSX_ARCHITECTURES=arm64;x86_64' ./compileMbedtls.sh
5555
- name: Archive artifacts
5656
uses: actions/upload-artifact@v5
5757
with:
@@ -74,9 +74,7 @@ jobs:
7474
MBEDTLS_VERSION: ${{ inputs.mbedtlsVersion }}
7575
run: |
7676
WINDOWS=1 \
77-
OBJEXT=obj \
7877
CMAKE_EXTRA='-DCMAKE_C_FLAGS=-D__USE_MINGW_ANSI_STDIO=0' \
79-
LDFLAGS='-lbcrypt -lws2_32 -lwinmm -lgdi32 -L. -static-libgcc' \
8078
DLEXT=dll \
8179
OSARCH=win32-x86-64 \
8280
./compileMbedtls.sh

Readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,15 @@ Linux (x86_64):
108108

109109
Mac (intel and arm):
110110

111-
`LDFLAGS='-arch x86_64 -arch arm64' CFLAGS='-O2 -arch x86_64 -arch arm64' DLEXT=dylib OSARCH=darwin ./compileMbedtls.sh`
111+
`DLEXT=dylib OSARCH=darwin CMAKE_EXTRA='-DCMAKE_OSX_ARCHITECTURES=arm64;x86_64' ./compileMbedtls.sh`
112112

113113
Windows
114114

115115
- `docker run -it -v$(pwd):/work --rm dockcross/windows-static-x64 \
116116
sh -c "apt-get update && apt-get install -y python3-venv && \
117117
WINDOWS=1 \
118-
LDFLAGS='-lws2_32 -lwinmm -lgdi32 -lbcrypt -L. -static-libgcc' \
119-
DLEXT=dll \
120-
OBJEXT=obj \
121118
CMAKE_EXTRA='-DCMAKE_C_FLAGS=-D__USE_MINGW_ANSI_STDIO=0' \
119+
DLEXT=dll \
122120
OSARCH=win32-x86-64 \
123121
./compileMbedtls.sh"`
124122

compileMbedtls.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ MBEDTLS_VERSION=${MBEDTLS_VERSION:-$DEFAULT_MBEDTLS_VERSION}
66
BUILD_DIR=mbedtls-lib/build/mbedtls-${MBEDTLS_VERSION}
77
DLEXT="${DLEXT:-so}"
88
OSARCH="${OSARCH:-linux-x86-64}"
9-
CC="${CC:-gcc}"
10-
LDFLAGS="${LDFLAGS:-}"
11-
OBJEXT="${OBJEXT:-o}"
129
CMAKE_EXTRA="${CMAKE_EXTRA:-}"
10+
LIB_DIR="mbedtls-lib/bin/$OSARCH"
1311

1412
# prepare build directory
1513
mkdir -p mbedtls-lib/build
@@ -31,22 +29,25 @@ fi
3129
python3 ${BUILD_DIR}/scripts/config.py -f "${BUILD_DIR}/include/mbedtls/mbedtls_config.h" unset MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
3230
python3 ${BUILD_DIR}/scripts/config.py -f "${BUILD_DIR}/include/mbedtls/mbedtls_config.h" set MBEDTLS_SSL_DTLS_CONNECTION_ID
3331

34-
# Run cmake configuration
35-
cmake -S "${BUILD_DIR}" -B "${BUILD_DIR}"/build -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release ${CMAKE_EXTRA}
32+
echo "Configuring CMake..."
33+
cmake \
34+
-S "${BUILD_DIR}" \
35+
-B "${BUILD_DIR}"/build \
36+
-DUSE_SHARED_MBEDTLS_LIBRARY=On \
37+
-DCMAKE_BUILD_TYPE=Release \
38+
${CMAKE_EXTRA}
3639

37-
cmake --build "${BUILD_DIR}"/build --target lib
40+
echo "Building MbedTLS..."
41+
cmake --build "${BUILD_DIR}"/build --parallel --target lib
3842

3943
# create single shared library
40-
LIB_DIR="mbedtls-lib/bin/$OSARCH"
4144
mkdir -p ${LIB_DIR}
4245
rm -f ${LIB_DIR}/* 2>/dev/null || true
4346

44-
$CC -shared \
45-
${BUILD_DIR}/build/library/CMakeFiles/mbedtls.dir/*.${OBJEXT} \
46-
${BUILD_DIR}/build/library/CMakeFiles/mbedx509.dir/*.${OBJEXT} \
47-
${BUILD_DIR}/build/tf-psa-crypto/core/CMakeFiles/tfpsacrypto.dir/*.${OBJEXT} \
48-
${BUILD_DIR}/build/tf-psa-crypto/drivers/builtin/CMakeFiles/builtin.dir/src/*.${OBJEXT} \
49-
-o ${LIB_DIR}/libmbedtls-${MBEDTLS_VERSION}.${DLEXT} ${LDFLAGS}
47+
# copy shared libraries
48+
cp "${BUILD_DIR}/build/library/libmbedtls.${DLEXT}" "${LIB_DIR}/libmbedtls.${DLEXT}"
49+
cp "${BUILD_DIR}/build/library/libmbedx509.${DLEXT}" "${LIB_DIR}/libmbedx509.${DLEXT}"
50+
cp "${BUILD_DIR}/build/library/libtfpsacrypto"*.${DLEXT} "${LIB_DIR}/libtfpsacrypto.${DLEXT}"
5051

5152
# generate kotlin object with memory sizes
5253
gcc mbedtls-lib/mbedtls_sizeof_generator.c \

kotlin-mbedtls-metrics/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ dependencies {
1414

1515
tasks.test {
1616
useJUnitPlatform()
17+
// On Windows, native libraries must be found via PATH or explicitly set, as dynamic linking is used to load them.
18+
if (System.getProperty("os.name").lowercase().contains("win")) {
19+
val osArch = "win32-x86-64"
20+
systemProperty("jna.library.path", file("../mbedtls-lib/bin/$osArch").absolutePath)
21+
}
1722
}

kotlin-mbedtls-netty/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ dependencies {
2222

2323
tasks.test {
2424
useJUnitPlatform()
25+
// On Windows, native libraries must be found via PATH or explicitly set, as dynamic linking is used to load them.
26+
if (System.getProperty("os.name").lowercase().contains("win")) {
27+
val osArch = "win32-x86-64"
28+
systemProperty("jna.library.path", file("../mbedtls-lib/bin/$osArch").absolutePath)
29+
}
2530
}

kotlin-mbedtls/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ dependencies {
2323

2424
tasks.test {
2525
useJUnitPlatform()
26+
// On Windows, native libraries must be found via PATH or explicitly set, as dynamic linking is used to load them.
27+
if (System.getProperty("os.name").lowercase().contains("win")) {
28+
val osArch = "win32-x86-64"
29+
systemProperty("jna.library.path", file("../mbedtls-lib/bin/$osArch").absolutePath)
30+
}
2631
}
2732

2833
jmh {

kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/MbedtlsApi.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,23 @@ import com.sun.jna.Function
2323
import com.sun.jna.Memory
2424
import com.sun.jna.Native
2525
import com.sun.jna.NativeLibrary
26-
import com.sun.jna.Platform
2726
import com.sun.jna.Pointer
2827
import org.slf4j.LoggerFactory
2928
import java.nio.ByteBuffer
30-
import java.util.Properties
3129

3230
/*
3331
Defines mbedtls native functions that can be used from jvm.
3432
*/
3533
internal object MbedtlsApi {
36-
private val libraryName = javaClass.classLoader.getResourceAsStream("mbedtls.properties").use { resource ->
37-
Properties().apply { load(resource) }.let { props ->
38-
val mbedtlsVersion = props.getProperty("mbedtlsVersion")
39-
if (Platform.isWindows()) "libmbedtls-$mbedtlsVersion" else "mbedtls-$mbedtlsVersion"
40-
}
41-
}
42-
private val LIB_MBEDTLS = NativeLibrary.getInstance(libraryName)
34+
35+
private var LIB_TFPSACRYPTO: NativeLibrary = NativeLibrary.getInstance("tfpsacrypto")
36+
private var LIB_MBEDX509: NativeLibrary = NativeLibrary.getInstance("mbedx509")
37+
private var LIB_MBEDTLS: NativeLibrary = NativeLibrary.getInstance("mbedtls")
4338

4439
init {
4540
Native.register(LIB_MBEDTLS)
46-
Native.register(Crypto::class.java, LIB_MBEDTLS)
47-
Native.register(X509::class.java, LIB_MBEDTLS)
41+
Native.register(X509::class.java, LIB_MBEDX509)
42+
Native.register(Crypto::class.java, LIB_TFPSACRYPTO)
4843

4944
configureLogThreshold()
5045
}
@@ -82,10 +77,6 @@ internal object MbedtlsApi {
8277
external fun mbedtls_ssl_set_mtu(sslContext: Pointer, mtu: Int)
8378
external fun mbedtls_ssl_get_peer_cert(sslContext: Pointer): Pointer?
8479
external fun mbedtls_ssl_set_hostname(sslContext: Pointer, hostname: String?): Int
85-
external fun psa_crypto_init(): Int
86-
87-
// mbedtls/error.h
88-
external fun mbedtls_strerror(errnum: Int, buffer: Pointer, buflen: Int)
8980

9081
const val MBEDTLS_ERR_SSL_TIMEOUT = -0x6800
9182
const val MBEDTLS_ERR_SSL_WANT_READ = -0x6900
@@ -140,6 +131,9 @@ internal object MbedtlsApi {
140131
external fun mbedtls_pk_init(ctx: Pointer)
141132
external fun mbedtls_pk_free(ctx: Pointer)
142133
external fun mbedtls_pk_parse_key(ctx: Pointer, key: ByteArray, keyLen: Int, pwd: Pointer?, pwdLen: Int): Int
134+
135+
// psa/crypto.h
136+
external fun psa_crypto_init(): Int
143137
}
144138

145139
internal object X509 {
@@ -148,5 +142,8 @@ internal object MbedtlsApi {
148142
external fun mbedtls_x509_crt_init(cert: Pointer)
149143
external fun mbedtls_x509_crt_free(cert: Pointer)
150144
external fun mbedtls_x509_crt_parse_der(chain: Pointer, buf: ByteArray, len: Int): Int
145+
146+
// mbedtls/error.h
147+
external fun mbedtls_strerror(errnum: Int, buffer: Pointer, buflen: Int)
151148
}
152149
}

kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/SslConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.sun.jna.Memory
2121
import com.sun.jna.Pointer
2222
import org.opencoap.ssl.MbedtlsApi.Crypto.mbedtls_pk_free
2323
import org.opencoap.ssl.MbedtlsApi.Crypto.mbedtls_pk_parse_key
24+
import org.opencoap.ssl.MbedtlsApi.Crypto.psa_crypto_init
2425
import org.opencoap.ssl.MbedtlsApi.X509.mbedtls_x509_crt_free
2526
import org.opencoap.ssl.MbedtlsApi.X509.mbedtls_x509_crt_parse_der
2627
import org.opencoap.ssl.MbedtlsApi.mbedtls_ssl_conf_authmode
@@ -47,7 +48,6 @@ import org.opencoap.ssl.MbedtlsApi.mbedtls_ssl_set_hostname
4748
import org.opencoap.ssl.MbedtlsApi.mbedtls_ssl_set_mtu
4849
import org.opencoap.ssl.MbedtlsApi.mbedtls_ssl_set_timer_cb
4950
import org.opencoap.ssl.MbedtlsApi.mbedtls_ssl_setup
50-
import org.opencoap.ssl.MbedtlsApi.psa_crypto_init
5151
import org.opencoap.ssl.MbedtlsApi.verify
5252
import org.slf4j.LoggerFactory
5353
import java.io.Closeable

kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/SslException.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.opencoap.ssl
1818

1919
import com.sun.jna.Memory
20+
import org.opencoap.ssl.MbedtlsApi.X509.mbedtls_strerror
2021
import java.util.Locale
2122

2223
open class SslException(message: String) : Exception(message) {
@@ -29,7 +30,7 @@ open class SslException(message: String) : Exception(message) {
2930

3031
internal fun translateError(error: Int): String {
3132
val buffer = Memory(100)
32-
MbedtlsApi.mbedtls_strerror(error, buffer, buffer.size().toInt())
33+
mbedtls_strerror(error, buffer, buffer.size().toInt())
3334
return buffer.getString(0).trim()
3435
}
3536
}

kotlin-mbedtls/src/main/resources/mbedtls.properties

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,3 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
17-
mbedtlsVersion=4.0.0

0 commit comments

Comments
 (0)