Skip to content

Commit a9ca898

Browse files
committed
feat(build): Improve OpenSSL libcrypto discovery
1 parent 18c0f26 commit a9ca898

File tree

4 files changed

+157
-22
lines changed

4 files changed

+157
-22
lines changed

.github/workflows/ci_c.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
name: C Code
3+
4+
on:
5+
push:
6+
branches: [main]
7+
pull_request:
8+
branches: [main]
9+
merge_group:
10+
types: [checks_requested]
11+
branches: [main]
12+
workflow_dispatch:
13+
14+
jobs:
15+
libcrypto-with-dependency:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v5
19+
20+
- name: Cache OpenSSL
21+
id: cache-openssl
22+
uses: actions/cache@v4
23+
with:
24+
path: ./openssl-3.5.4/openssl-install
25+
key: ${{ runner.os }}-openssl-3.5.4
26+
27+
- name: Install OpenSSL
28+
if: ${{ steps.cache-openssl.outputs.cache-hit != 'true' }}
29+
run: |
30+
wget https://github.com/openssl/openssl/releases/download/openssl-3.5.4/openssl-3.5.4.tar.gz
31+
tar -xzvf openssl-3.5.4.tar.gz
32+
33+
pushd openssl-3.5.4
34+
install_dir="$(pwd)"/openssl-install
35+
mkdir "${install_dir}"
36+
37+
# Build OpenSSL with a dependency on zlib.
38+
./config \
39+
--prefix="${install_dir}" \
40+
--openssldir="${install_dir}/openssl" \
41+
--libdir=lib \
42+
zlib
43+
make
44+
make install
45+
46+
ls
47+
48+
# Ensure the built libcrypto has a dependency on zlib.
49+
ldd "${install_dir}"/lib/libcrypto.so | grep libz.so
50+
51+
popd
52+
53+
- name: Build static
54+
run: |
55+
libcrypto_dir="$(pwd)/openssl-3.5.4/openssl-install"
56+
cmake . -Bbuild \
57+
-DCMAKE_PREFIX_PATH="${libcrypto_dir}" \
58+
-DBUILD_SHARED_LIBS=0
59+
60+
jobs=$(nproc)
61+
cmake --build build -j $jobs
62+
CTEST_PARALLEL_LEVEL=$jobs ctest --test-dir build
63+
64+
- name: Build shared
65+
run: |
66+
libcrypto_dir="$(pwd)/openssl-3.5.4/openssl-install"
67+
cmake . -Bbuild \
68+
-DCMAKE_PREFIX_PATH="${libcrypto_dir}" \
69+
-DBUILD_SHARED_LIBS=1
70+
71+
jobs=$(nproc)
72+
cmake --build build -j $jobs
73+
CTEST_PARALLEL_LEVEL=$jobs ctest --test-dir build
74+
75+
- name: Build with interning
76+
run: |
77+
libcrypto_dir="$(pwd)/openssl-3.5.4/openssl-install"
78+
cmake . -Bbuild \
79+
-DCMAKE_PREFIX_PATH="${libcrypto_dir}" \
80+
-DS2N_INTERN_LIBCRYPTO=1
81+
82+
jobs=$(nproc)
83+
cmake --build build -j $jobs
84+
CTEST_PARALLEL_LEVEL=$jobs ctest --test-dir build

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,15 @@ if (S2N_INTERN_LIBCRYPTO)
432432
add_dependencies(${PROJECT_NAME} s2n_libcrypto)
433433
add_definitions(-DS2N_INTERN_LIBCRYPTO=1)
434434

435+
# Now that the libcrypto lives inside libs2n, any dependencies of the libcrypto must become
436+
# dependencies of libs2n.
437+
get_target_property(crypto_LINK_LIBRARIES AWS::crypto INTERFACE_LINK_LIBRARIES)
438+
if (crypto_LINK_LIBRARIES)
439+
foreach (link_library ${crypto_LINK_LIBRARIES})
440+
target_link_libraries(${PROJECT_NAME} PUBLIC ${link_library})
441+
endforeach()
442+
endif()
443+
435444
if ((BUILD_SHARED_LIBS AND BUILD_TESTING) OR NOT BUILD_SHARED_LIBS)
436445
# if libcrypto needs to be interned, rewrite libcrypto references so use of internal functions will link correctly
437446
add_custom_command(

cmake/modules/Findcrypto.cmake

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,49 @@ if (TARGET crypto OR TARGET AWS::crypto)
3131
set(CRYPTO_FOUND true)
3232
set(crypto_FOUND true)
3333
else()
34-
find_path(crypto_INCLUDE_DIR
35-
NAMES openssl/crypto.h
36-
HINTS
37-
"${CMAKE_PREFIX_PATH}"
38-
"${CMAKE_INSTALL_PREFIX}"
39-
PATH_SUFFIXES include
40-
)
34+
if(NOT BUILD_SHARED_LIBS AND NOT S2N_USE_CRYPTO_SHARED_LIBS)
35+
set(OPENSSL_USE_STATIC_LIBS TRUE)
36+
endif()
37+
find_package(OpenSSL COMPONENTS Crypto QUIET)
38+
if (OpenSSL_FOUND)
39+
set(crypto_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
40+
set(crypto_LIBRARY ${OPENSSL_CRYPTO_LIBRARY})
4141

42-
find_library(crypto_SHARED_LIBRARY
43-
NAMES libcrypto.so libcrypto.dylib
44-
HINTS
45-
"${CMAKE_PREFIX_PATH}"
46-
"${CMAKE_INSTALL_PREFIX}"
47-
PATH_SUFFIXES build/crypto build lib64 lib
48-
)
42+
# CMakeLists.txt expects the crypto_SHARED_LIBRARY/crypto_STATIC_LIBRARY variables to be
43+
# set. However, the FindOpenSSL module doesn't set separate variables depending on whether
44+
# the artifact is shared or static. And, the TYPE property isn't set on the OpenSSL::Crypto
45+
# target, so we can't use `get_target_property()` to determine this. Instead, we check for
46+
# a ".a" suffix in the artifact path.
47+
if ("${crypto_LIBRARY}" MATCHES "\\.a$")
48+
set(crypto_STATIC_LIBRARY "${crypto_LIBRARY}")
49+
else()
50+
set(crypto_SHARED_LIBRARY "${crypto_LIBRARY}")
51+
endif()
52+
else()
53+
find_path(crypto_INCLUDE_DIR
54+
NAMES openssl/crypto.h
55+
HINTS
56+
"${CMAKE_PREFIX_PATH}"
57+
"${CMAKE_INSTALL_PREFIX}"
58+
PATH_SUFFIXES include
59+
)
4960

50-
find_library(crypto_STATIC_LIBRARY
51-
NAMES libcrypto.a
52-
HINTS
53-
"${CMAKE_PREFIX_PATH}"
54-
"${CMAKE_INSTALL_PREFIX}"
55-
PATH_SUFFIXES build/crypto build lib64 lib
56-
)
61+
find_library(crypto_SHARED_LIBRARY
62+
NAMES libcrypto.so libcrypto.dylib
63+
HINTS
64+
"${CMAKE_PREFIX_PATH}"
65+
"${CMAKE_INSTALL_PREFIX}"
66+
PATH_SUFFIXES build/crypto build lib64 lib
67+
)
68+
69+
find_library(crypto_STATIC_LIBRARY
70+
NAMES libcrypto.a
71+
HINTS
72+
"${CMAKE_PREFIX_PATH}"
73+
"${CMAKE_INSTALL_PREFIX}"
74+
PATH_SUFFIXES build/crypto build lib64 lib
75+
)
76+
endif()
5777

5878
if (NOT crypto_LIBRARY)
5979
if (BUILD_SHARED_LIBS OR S2N_USE_CRYPTO_SHARED_LIBS)
@@ -92,6 +112,12 @@ else()
92112
set(CRYPTO_FOUND true)
93113
set(crypto_FOUND true)
94114

115+
if (TARGET OpenSSL::Crypto)
116+
message(STATUS "libcrypto discovered by the FindOpenSSL module")
117+
else()
118+
message(STATUS "libcrypto discovered by the s2n-tls Findcrypto module")
119+
endif()
120+
95121
message(STATUS "LibCrypto Include Dir: ${crypto_INCLUDE_DIR}")
96122
message(STATUS "LibCrypto Shared Lib: ${crypto_SHARED_LIBRARY}")
97123
message(STATUS "LibCrypto Static Lib: ${crypto_STATIC_LIBRARY}")
@@ -107,6 +133,19 @@ else()
107133
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
108134
IMPORTED_LOCATION "${crypto_LIBRARY}")
109135
add_dependencies(AWS::crypto Threads::Threads)
136+
137+
if (TARGET OpenSSL::Crypto)
138+
# The discovered libcrypto may have been configured with additional dependencies
139+
# such as zlib. If any dependencies were discovered by the FindOpenSSL module, add
140+
# them to the AWS::crypto target.
141+
get_target_property(OpenSSL_LINK_LIBRARIES OpenSSL::Crypto
142+
INTERFACE_LINK_LIBRARIES)
143+
if (OpenSSL_LINK_LIBRARIES)
144+
foreach (link_library ${OpenSSL_LINK_LIBRARIES})
145+
target_link_libraries(AWS::crypto INTERFACE ${link_library})
146+
endforeach()
147+
endif()
148+
endif()
110149
endif()
111150
endif()
112151

codebuild/spec/buildspec_32bit_cross_compile.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ phases:
2323
build:
2424
on-failure: ABORT
2525
commands:
26-
- cmake . -Bbuild -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/32-bit.toolchain
26+
- |
27+
cmake . -Bbuild \
28+
-DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/32-bit.toolchain \
29+
-DCMAKE_PREFIX_PATH=/usr/lib/i386-linux-gnu
2730
- cmake --build ./build -j $(nproc)
2831
post_build:
2932
on-failure: ABORT

0 commit comments

Comments
 (0)