Skip to content

Commit

Permalink
Clear up usage of static libraries.
Browse files Browse the repository at this point in the history
- Python bindings only use the dynamic lib. But built and copied the static ones sometimes nonetheless.
- Add toggles to build only static, static/dyn or only dynamic.
  • Loading branch information
Rot127 committed Oct 23, 2024
1 parent 07491d6 commit 16efa22
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 58 deletions.
37 changes: 26 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ endif()
# to configure the options specify them in the command line or change them in the cmake UI.
# Don't edit the makefile!
option(BUILD_SHARED_LIBS "Build shared library" OFF)
option(CAPSTONE_BUILD_STATIC_RUNTIME "Embed static runtime" ${BUILD_SHARED_LIBS})
option(BUILD_STATIC_LIBS "Build static library" ON)
option(BUILD_STATIC_RUNTIME "Embed static MSVC runtime (Windows only). Always set if BUILD_SHARED_LIBS=ON" ${BUILD_SHARED_LIBS})
option(CAPSTONE_BUILD_MACOS_THIN "Disable universal2 builds on macOS" OFF)
option(CAPSTONE_BUILD_DIET "Build diet library" OFF)
option(CAPSTONE_BUILD_LEGACY_TESTS "Build legacy tests" ${PROJECT_IS_TOP_LEVEL})
Expand All @@ -69,6 +70,10 @@ option(CAPSTONE_INSTALL "Generate install target" ${PROJECT_IS_TOP_LEVEL})
option(ENABLE_ASAN "Enable address sanitizer" OFF)
option(ENABLE_COVERAGE "Enable test coverage" OFF)

if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
FATAL_ERROR("BUILD_SHARED_LIBS and BUILD_STATIC_LIBS are both unset. Nothing to build.")
endif()

if (ENABLE_ASAN)
message("Enabling ASAN")
add_definitions(-DASAN_ENABLED)
Expand Down Expand Up @@ -154,7 +159,7 @@ if(CAPSTONE_DEBUG OR CMAKE_BUILD_TYPE STREQUAL "Debug")
endif()

# Force static runtime libraries
if(CAPSTONE_BUILD_STATIC_RUNTIME)
if(BUILD_STATIC_RUNTIME)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

Expand Down Expand Up @@ -762,25 +767,33 @@ set_property(GLOBAL PROPERTY VERSION ${PROJECT_VERSION})

## targets
add_library(capstone OBJECT ${ALL_SOURCES} ${ALL_HEADERS})
add_library(capstone::capstone ALIAS capstone)
add_library(capstone_static STATIC $<TARGET_OBJECTS:capstone>)
# Use normal capstone name. Otherwise we get libcapstone_static.so
set_target_properties(capstone_static PROPERTIES OUTPUT_NAME "capstone")
set_property(TARGET capstone PROPERTY C_STANDARD 99)
target_include_directories(capstone PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)
set_property(TARGET capstone PROPERTY C_STANDARD 99)

if(BUILD_STATIC_LIBS)
add_library(capstone_static STATIC $<TARGET_OBJECTS:capstone>)
# Use normal capstone name. Otherwise we get libcapstone_static.a
set_target_properties(capstone_static PROPERTIES OUTPUT_NAME "capstone")
target_include_directories(capstone_static PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)
endif()

if(BUILD_SHARED_LIBS)
set_property(TARGET capstone PROPERTY POSITION_INDEPENDENT_CODE 1)
add_library(capstone_shared SHARED $<TARGET_OBJECTS:capstone>)
# Use normal capstone name. Otherwise we get libcapstone_shared.so
set_target_properties(capstone_shared PROPERTIES OUTPUT_NAME "capstone")
target_compile_definitions(capstone_shared PUBLIC CAPSTONE_SHARED)
set_target_properties(capstone_shared PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
target_include_directories(capstone_shared PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)
target_compile_definitions(capstone PUBLIC CAPSTONE_SHARED)
endif()

# Fuzzer if this is moved to it's own CMakeLists.txt (as it should be)
Expand Down Expand Up @@ -886,9 +899,11 @@ if(CAPSTONE_INSTALL)
)

if(BUILD_SHARED_LIBS)
set(LIB_INSTALL_TARGETS capstone_static capstone_shared)
else()
set(LIB_INSTALL_TARGETS capstone_static)
set(LIB_INSTALL_TARGETS capstone_shared)
endif()

if (BUILD_STATIC_LIBS)
set(LIB_INSTALL_TARGETS ${LIB_INSTALL_TARGETS} capstone_static)
endif()

install(TARGETS ${LIB_INSTALL_TARGETS}
Expand Down
16 changes: 3 additions & 13 deletions bindings/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,12 @@
if SYSTEM == 'darwin':
VERSIONED_LIBRARY_FILE = "libcapstone.{PKG_MAJOR}.dylib".format(**VERSION_DATA)
LIBRARY_FILE = "libcapstone.dylib"
STATIC_LIBRARY_FILE = 'libcapstone.a'
elif SYSTEM in ('win32', 'cygwin'):
VERSIONED_LIBRARY_FILE = "capstone.dll"
LIBRARY_FILE = "capstone.dll"
STATIC_LIBRARY_FILE = None
else:
VERSIONED_LIBRARY_FILE = "libcapstone.so.{PKG_MAJOR}".format(**VERSION_DATA)
LIBRARY_FILE = "libcapstone.so"
STATIC_LIBRARY_FILE = 'libcapstone.a'


def clean_bins():
Expand Down Expand Up @@ -123,12 +120,9 @@ def build_libraries():
shutil.copytree(os.path.join(BUILD_DIR, 'include', 'capstone'), os.path.join(HEADERS_DIR, 'capstone'))

# if prebuilt libraries are available, use those and cancel build
if os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE)) and \
(not STATIC_LIBRARY_FILE or os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE))):
if os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE)):
logger.info('Using prebuilt libraries')
shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE), LIBS_DIR)
if STATIC_LIBRARY_FILE is not None:
shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE), LIBS_DIR)
return

os.chdir(BUILD_DIR)
Expand All @@ -141,8 +135,8 @@ def build_libraries():
os.chdir("build_py")
print("Build Directory: {}\n".format(os.getcwd()))
# Only build capstone.dll / libcapstone.dylib
if SYSTEM == "win32":
os.system('cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCAPSTONE_BUILD_LEGACY_TESTS=OFF -DCAPSTONE_BUILD_CSTOOL=OFF -G "NMake Makefiles" ..')
if SYSTEM in ('win32', 'cygwin'):
os.system('cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=OFF -DCAPSTONE_BUILD_LEGACY_TESTS=OFF -DCAPSTONE_BUILD_CSTOOL=OFF -G "NMake Makefiles" ..')
elif 'AFL_NOOPT' in os.environ:
# build for test_corpus
os.system('cmake -DBUILD_SHARED_LIBS=ON -DCAPSTONE_BUILD_LEGACY_TESTS=OFF -DCAPSTONE_BUILD_CSTOOL=OFF ..')
Expand All @@ -151,10 +145,6 @@ def build_libraries():
os.system("cmake --build .")

shutil.copy(VERSIONED_LIBRARY_FILE, os.path.join(LIBS_DIR, LIBRARY_FILE))

# only copy static library if it exists (it's a build option)
if STATIC_LIBRARY_FILE and os.path.exists(STATIC_LIBRARY_FILE):
shutil.copy(STATIC_LIBRARY_FILE, LIBS_DIR)
os.chdir(cwd)


Expand Down
34 changes: 0 additions & 34 deletions nmake.bat

This file was deleted.

0 comments on commit 16efa22

Please sign in to comment.