@@ -21,9 +21,16 @@ cmake_policy(SET CMP0042 NEW)
2121# Enable support for MSVC_RUNTIME_LIBRARY
2222cmake_policy (SET CMP0091 NEW)
2323
24- project (capstone
25- VERSION 5.0.3
26- )
24+ # Check if VERSION is provided externally, otherwise default to 5.0.3
25+ if (NOT DEFINED PROJECT_VERSION)
26+ set (PROJECT_VERSION "5.0.3" )
27+ endif ()
28+
29+ # Extract the major, minor, and patch versions
30+ string (REGEX MATCH "^[0-9]+\\ .[0-9]+\\ .[0-9]+" PROJECT_VERSION_BASE ${PROJECT_VERSION} )
31+
32+ # Set the project version without the pre-release identifier
33+ project (capstone VERSION ${PROJECT_VERSION_BASE} )
2734
2835if (MSVC )
2936 add_compile_options (/W1 /w14189)
@@ -35,7 +42,8 @@ endif()
3542# to configure the options specify them in in the command line or change them in the cmake UI.
3643# Don't edit the makefile!
3744option (BUILD_SHARED_LIBS "Build shared library" OFF )
38- option (CAPSTONE_BUILD_STATIC_RUNTIME "Embed static runtime" ${BUILD_SHARED_LIBS} )
45+ option (BUILD_STATIC_LIBS "Build static library" ON )
46+ option (BUILD_STATIC_RUNTIME "Embed static MSVC runtime (Windows only). Always set if BUILD_SHARED_LIBS=ON" ${BUILD_SHARED_LIBS} )
3947option (CAPSTONE_BUILD_MACOS_THIN "Disable universal2 builds on macOS" OFF )
4048option (CAPSTONE_BUILD_DIET "Build diet library" OFF )
4149option (CAPSTONE_BUILD_TESTS "Build tests" ${PROJECT_IS_TOP_LEVEL} )
@@ -46,6 +54,10 @@ option(CAPSTONE_ARCHITECTURE_DEFAULT "Whether architectures are enabled by defau
4654option (CAPSTONE_DEBUG "Whether to enable extra debug assertions" OFF )
4755option (CAPSTONE_INSTALL "Generate install target" ${PROJECT_IS_TOP_LEVEL} )
4856
57+ if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
58+ FATAL_ERROR("BUILD_SHARED_LIBS and BUILD_STATIC_LIBS are both unset. Nothing to build." )
59+ endif ()
60+
4961set (SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TRICORE)
5062set (SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore)
5163
@@ -109,7 +121,7 @@ if(CAPSTONE_DEBUG)
109121endif ()
110122
111123# Force static runtime libraries
112- if (CAPSTONE_BUILD_STATIC_RUNTIME )
124+ if (BUILD_STATIC_RUNTIME )
113125 set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" )
114126endif ()
115127
@@ -631,19 +643,33 @@ set(ALL_HEADERS
631643set_property (GLOBAL PROPERTY VERSION ${PROJECT_VERSION} )
632644
633645## targets
634- add_library (capstone ${ALL_SOURCES} ${ALL_HEADERS} )
635- add_library ( capstone::capstone ALIAS capstone )
646+ add_library (capstone OBJECT ${ALL_SOURCES} ${ALL_HEADERS} )
647+ set_property ( TARGET capstone PROPERTY C_STANDARD 99 )
636648target_include_directories (capstone PUBLIC
637649 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR} /include >
638650)
639- set_property (TARGET capstone PROPERTY C_STANDARD 99)
651+ if (BUILD_STATIC_LIBS)
652+ add_library (capstone_static STATIC $<TARGET_OBJECTS:capstone>)
653+ # Use normal capstone name. Otherwise we get libcapstone_static.a
654+ set_target_properties (capstone_static PROPERTIES OUTPUT_NAME "capstone" )
655+ target_include_directories (capstone_static PUBLIC
656+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR} /include >
657+ )
658+ endif ()
640659
641660if (BUILD_SHARED_LIBS )
642- target_compile_definitions (capstone PUBLIC CAPSTONE_SHARED)
643- set_target_properties (capstone PROPERTIES
661+ set_property (TARGET capstone PROPERTY POSITION_INDEPENDENT_CODE 1)
662+ add_library (capstone_shared SHARED $<TARGET_OBJECTS:capstone>)
663+ # Use normal capstone name. Otherwise we get libcapstone_shared.so
664+ set_target_properties (capstone_shared PROPERTIES OUTPUT_NAME "capstone" )
665+ set_target_properties (capstone_shared PROPERTIES
644666 VERSION ${PROJECT_VERSION}
645667 SOVERSION ${PROJECT_VERSION_MAJOR}
646668 )
669+ target_include_directories (capstone_shared PUBLIC
670+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR} /include >
671+ )
672+ target_compile_definitions (capstone PUBLIC CAPSTONE_SHARED)
647673endif ()
648674
649675if (CAPSTONE_BUILD_TESTS)
@@ -712,7 +738,6 @@ source_group("Include\\TriCore" FILES ${HEADERS_TRICORE})
712738## installation
713739if (CAPSTONE_INSTALL)
714740 include (GNUInstallDirs)
715-
716741 install (FILES ${HEADERS_COMMON} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /capstone)
717742
718743 # Support absolute installation paths (discussion: https://github.com/NixOS/nixpkgs/issues/144170)
@@ -753,12 +778,20 @@ if(CAPSTONE_INSTALL)
753778 DESTINATION ${CAPSTONE_CMAKE_CONFIG_INSTALL_DIR}
754779 )
755780
756- install (TARGETS capstone
757- EXPORT capstone-targets
758- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
759- LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
760- ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
761- INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
781+ if (BUILD_SHARED_LIBS )
782+ set (LIB_INSTALL_TARGETS capstone_shared)
783+ endif ()
784+
785+ if (BUILD_STATIC_LIBS)
786+ set (LIB_INSTALL_TARGETS ${LIB_INSTALL_TARGETS} capstone_static)
787+ endif ()
788+
789+ install (TARGETS ${LIB_INSTALL_TARGETS}
790+ EXPORT capstone-targets
791+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
792+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
793+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
794+ INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
762795 )
763796
764797 install (EXPORT capstone-targets
0 commit comments