Skip to content

Commit

Permalink
find_package(Threads) regardless of BUILD_ONLY_LIB (#1653)
Browse files Browse the repository at this point in the history
* find_package(Threads) regardless of BUILD_ONLY_LIB

* New macro OQS_USE_PTHREADS conditioned on embedded
  • Loading branch information
zxjtan authored Jan 31, 2024
1 parent 6bce010 commit 688bdb4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
14 changes: 8 additions & 6 deletions .CMake/compiler_opts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
add_link_options("-Wl,-z,noexecstack")
endif()

if(NOT ${OQS_BUILD_ONLY_LIB})
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
if (CMAKE_USE_PTHREADS_INIT AND NOT OQS_EMBEDDED_BUILD)
set(OQS_USE_PTHREADS ON)
endif()

if(${OQS_DEBUG_BUILD})
Expand Down Expand Up @@ -165,9 +166,10 @@ elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
endif()
endif()

if(NOT ${OQS_BUILD_ONLY_LIB})
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
if (CMAKE_USE_PTHREADS_INIT AND NOT OQS_EMBEDDED_BUILD)
set(OQS_USE_PTHREADS ON)
endif()

if(${OQS_DEBUG_BUILD})
Expand Down
2 changes: 1 addition & 1 deletion src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ else()
target_compile_definitions(internal PRIVATE OQS_HAVE_GETENTROPY)
endif()
endif()
if(CMAKE_USE_PTHREADS_INIT)
if(OQS_USE_PTHREADS)
target_link_libraries(common PRIVATE Threads::Threads)
target_link_libraries(internal PRIVATE Threads::Threads)
endif()
Expand Down
6 changes: 3 additions & 3 deletions src/common/sha3/xkcp_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <oqs/common.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif
#include <stddef.h>
Expand All @@ -26,7 +26,7 @@
#define KECCAK_CTX_BYTES (KECCAK_CTX_ALIGNMENT * \
((_KECCAK_CTX_BYTES + KECCAK_CTX_ALIGNMENT - 1)/KECCAK_CTX_ALIGNMENT))

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT;
#endif

Expand Down Expand Up @@ -85,7 +85,7 @@ static void Keccak_Dispatch(void) {
* that have not been permuted, or not-yet-squeezed bytes.
**************************************************/
static void keccak_inc_reset(uint64_t *s) {
#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
pthread_once(&dispatch_once_control, Keccak_Dispatch);
#else
if (Keccak_Initialize_ptr == NULL) {
Expand Down
6 changes: 3 additions & 3 deletions src/common/sha3/xkcp_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <oqs/common.h>
#include <oqs/oqsconfig.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif
#include <stddef.h>
Expand All @@ -21,7 +21,7 @@
#define KECCAK_X4_CTX_BYTES (KECCAK_X4_CTX_ALIGNMENT * \
((_KECCAK_X4_CTX_BYTES + KECCAK_X4_CTX_ALIGNMENT - 1)/KECCAK_X4_CTX_ALIGNMENT))

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT;
#endif

Expand Down Expand Up @@ -65,7 +65,7 @@ static void Keccak_X4_Dispatch(void) {
}

static void keccak_x4_inc_reset(uint64_t *s) {
#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
pthread_once(&dispatch_once_control, Keccak_X4_Dispatch);
#else
if (Keccak_X4_Initialize_ptr == NULL) {
Expand Down
4 changes: 2 additions & 2 deletions src/oqsconfig.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
#cmakedefine USE_SANITIZER "@USE_SANITIZER@"
#cmakedefine CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@"

#cmakedefine CMAKE_USE_PTHREADS_INIT 1

#cmakedefine OQS_USE_OPENSSL 1
#cmakedefine OQS_USE_AES_OPENSSL 1
#cmakedefine OQS_USE_SHA2_OPENSSL 1
#cmakedefine OQS_USE_SHA3_OPENSSL 1

#cmakedefine OQS_EMBEDDED_BUILD 1

#cmakedefine OQS_USE_PTHREADS 1

#cmakedefine OQS_USE_ADX_INSTRUCTIONS 1
#cmakedefine OQS_USE_AES_INSTRUCTIONS 1
#cmakedefine OQS_USE_AVX_INSTRUCTIONS 1
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ endif()

# List oqs-internal after oqs so that oqs has linking precedence.
set(TEST_DEPS oqs oqs-internal ${LIBM})
if(CMAKE_USE_PTHREADS_INIT)
if(OQS_USE_PTHREADS)
set(TEST_DEPS ${TEST_DEPS} Threads::Threads)
endif()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <oqs/oqs.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif

Expand Down Expand Up @@ -197,7 +197,7 @@ static void TEST_KEM_randombytes(uint8_t *random_array, size_t bytes_to_read) {
}
#endif

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
struct thread_data {
char *alg_name;
OQS_STATUS rc;
Expand Down Expand Up @@ -245,7 +245,7 @@ int main(int argc, char **argv) {
#endif

OQS_STATUS rc;
#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#define MAX_LEN_KEM_NAME_ 64
// don't run Classic McEliece in threads because of large stack usage
char no_thread_kem_patterns[][MAX_LEN_KEM_NAME_] = {"Classic-McEliece", "HQC-256-"};
Expand Down
6 changes: 3 additions & 3 deletions tests/test_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <oqs/oqs.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif

Expand Down Expand Up @@ -174,7 +174,7 @@ static void TEST_SIG_randombytes(uint8_t *random_array, size_t bytes_to_read) {
}
#endif

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
struct thread_data {
char *alg_name;
OQS_STATUS rc;
Expand Down Expand Up @@ -222,7 +222,7 @@ int main(int argc, char **argv) {
#endif

OQS_STATUS rc;
#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#define MAX_LEN_SIG_NAME_ 64
pthread_t thread;
struct thread_data td;
Expand Down

0 comments on commit 688bdb4

Please sign in to comment.