Skip to content

Commit 11d854f

Browse files
committed
Make static build controlled by config option
1 parent 0f64e16 commit 11d854f

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ option(QIREE_BUILD_TESTS "Build QIR-EE unit tests" ON)
3838
option(QIREE_BUILD_EXAMPLES "Build QIR-EE examples" OFF)
3939
option(QIREE_USE_QSIM "Download and build Google qsim backend" OFF)
4040
option(QIREE_USE_XACC "Build XACC interface" ON)
41+
option(QIREE_SHARED_LIBS "Build shared libraries, not static libraries" ON)
4142

4243
qiree_set_default(BUILD_TESTING ${QIREE_BUILD_TESTS})
4344

@@ -77,10 +78,10 @@ qiree_set_default(CMAKE_CXX_EXTENSIONS OFF)
7778

7879
### Linking flags ###
7980
# Default to building shared libraries (*not* a cache variable)
80-
qiree_set_default(BUILD_SHARED_LIBS ON)
81+
qiree_set_default(BUILD_SHARED_LIBS ${QIREE_SHARED_LIBS})
8182
# Inform installed binaries of external library rpaths
8283
qiree_set_default(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
83-
if(BUILD_SHARED_LIBS)
84+
if(QIREE_SHARED_LIBS)
8485
# Inform installed binaries of internal library rpaths
8586
qiree_set_default(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
8687
# Do not relink libs/binaries when dependent shared libs change

CMakePresets.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
"binaryDir": "${sourceDir}/build-${presetName}",
1414
"generator": "Ninja",
1515
"cacheVariables": {
16-
"BUILD_SHARED_LIBS": {
17-
"type": "BOOL",
18-
"value": "ON"
19-
},
2016
"CMAKE_BUILD_TYPE": {
2117
"type": "STRING",
2218
"value": "Debug"

src/cqiree/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ if(QIREE_USE_QSIM)
1212
list(APPEND _CQIREE_LIBS QIREE::qirqsim)
1313
endif()
1414

15-
qiree_add_library(cqiree
15+
if(QIREE_SHARED_LIBS)
16+
# When building with shared libs, this should be a shared library that can
17+
# be dlopen()'d.
18+
set(_LIB_TYPE MODULE)
19+
else()
20+
set(_LIB_TYPE)
21+
endif()
22+
23+
qiree_add_library(cqiree ${_LIB_TYPE}
1624
CQiree.cc
1725
QireeManager.cc
1826
)

src/qiree_config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
#cmakedefine01 QIREE_DEBUG
1313
#cmakedefine01 QIREE_USE_QSIM
1414
#cmakedefine01 QIREE_USE_XACC
15+
#cmakedefine01 QIREE_SHARED_LIBS
1516

1617
#endif /* qiree_config_h */

test/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ qiree_add_test(qiree ResultDistribution)
6969
#---------------------------------------------------------------------------##
7070

7171
qiree_add_test(cqiree CQiree)
72-
add_dependencies(cqiree_CQireeTest cqiree)
72+
if(QIREE_SHARED_LIBS)
73+
# Tester uses dlopen()
74+
add_dependencies(cqiree_CQireeTest cqiree)
75+
else()
76+
# Statically link tester with cqiree
77+
target_link_libraries(cqiree_CQireeTest cqiree)
78+
endif()
7379

7480
#---------------------------------------------------------------------------##
7581
# QIRXACC TESTS

test/cqiree/CQiree.test.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <vector>
1616
#include <dlfcn.h>
1717

18+
#include "qiree_config.h"
1819
#include "qiree_targets.h"
1920

2021
#include "qiree/Assert.hh"
@@ -44,15 +45,21 @@ class CQireeTest : public ::qiree::test::Test
4445
protected:
4546
void SetUp() override
4647
{
48+
#if QIREE_SHARED_LIBS
4749
lib_handle_ = dlopen(cqiree_library_path, RTLD_LAZY);
4850
ASSERT_NE(lib_handle_, nullptr)
4951
<< "Failed to load libcqiree: " << dlerror();
50-
51-
#define LOAD_FUNCPTR(FUNC) \
52-
FUNC##_fn_ = reinterpret_cast<qiree_##FUNC##_t>( \
53-
dlsym(lib_handle_, "qiree_" #FUNC)); \
54-
ASSERT_NE(FUNC##_fn_, nullptr) \
55-
<< "Failed to load " << #FUNC << ": " << dlerror();
52+
#endif
53+
54+
#if QIREE_SHARED_LIBS
55+
# define LOAD_FUNCPTR(FUNC) \
56+
FUNC##_fn_ = reinterpret_cast<qiree_##FUNC##_t>( \
57+
dlsym(lib_handle_, "qiree_" #FUNC)); \
58+
ASSERT_NE(FUNC##_fn_, nullptr) \
59+
<< "Failed to load " << #FUNC << ": " << dlerror();
60+
#else
61+
# define LOAD_FUNCPTR(FUNC) FUNC##_fn_ = qiree_##FUNC;
62+
#endif
5663
LOAD_FUNCPTR(create);
5764
LOAD_FUNCPTR(load_module_from_memory);
5865
LOAD_FUNCPTR(load_module_from_file);

0 commit comments

Comments
 (0)