diff --git a/.gitattributes b/.gitattributes index 03e638de691..e7ea118ba25 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,4 @@ /arch/**/*.inc linguist-language=C + +# Ensure shell scripts have LF line endings +*.sh text eol=lf \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a0a70bb3cea..7ad0ed804ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,8 @@ endif() # to configure the options specify them in 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_TESTS "Build tests" ${PROJECT_IS_TOP_LEVEL}) @@ -46,6 +47,10 @@ option(CAPSTONE_ARCHITECTURE_DEFAULT "Whether architectures are enabled by defau option(CAPSTONE_DEBUG "Whether to enable extra debug assertions" OFF) option(CAPSTONE_INSTALL "Generate install target" ${PROJECT_IS_TOP_LEVEL}) +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() + set(SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TRICORE) set(SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore) @@ -109,7 +114,7 @@ if(CAPSTONE_DEBUG) endif() # Force static runtime libraries -if(CAPSTONE_BUILD_STATIC_RUNTIME) +if(BUILD_STATIC_RUNTIME) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") endif() @@ -631,19 +636,33 @@ set(ALL_HEADERS set_property(GLOBAL PROPERTY VERSION ${PROJECT_VERSION}) ## targets -add_library(capstone ${ALL_SOURCES} ${ALL_HEADERS}) -add_library(capstone::capstone ALIAS capstone) +add_library(capstone OBJECT ${ALL_SOURCES} ${ALL_HEADERS}) +set_property(TARGET capstone PROPERTY C_STANDARD 99) target_include_directories(capstone PUBLIC $ ) -set_property(TARGET capstone PROPERTY C_STANDARD 99) +if(BUILD_STATIC_LIBS) + add_library(capstone_static STATIC $) + # 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 + $ + ) +endif() if(BUILD_SHARED_LIBS) - target_compile_definitions(capstone PUBLIC CAPSTONE_SHARED) - set_target_properties(capstone PROPERTIES + set_property(TARGET capstone PROPERTY POSITION_INDEPENDENT_CODE 1) + add_library(capstone_shared SHARED $) + # Use normal capstone name. Otherwise we get libcapstone_shared.so + set_target_properties(capstone_shared PROPERTIES OUTPUT_NAME "capstone") + set_target_properties(capstone_shared PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) + target_include_directories(capstone_shared PUBLIC + $ + ) + target_compile_definitions(capstone PUBLIC CAPSTONE_SHARED) endif() if(CAPSTONE_BUILD_TESTS) @@ -753,12 +772,20 @@ if(CAPSTONE_INSTALL) DESTINATION ${CAPSTONE_CMAKE_CONFIG_INSTALL_DIR} ) - install(TARGETS capstone - EXPORT capstone-targets - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + if(BUILD_SHARED_LIBS) + 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} + EXPORT capstone-targets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) install(EXPORT capstone-targets diff --git a/bindings/python/setup.py b/bindings/python/setup.py index a5cb08c8be9..ef1eeb9a2d9 100755 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -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(): shutil.rmtree(LIBS_DIR, ignore_errors=True) @@ -124,12 +121,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) @@ -145,8 +139,8 @@ def build_libraries(): os.chdir("build") 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_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_TESTS=OFF -DCAPSTONE_BUILD_CSTOOL=OFF -G "NMake Makefiles" ..') else: os.system('cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCAPSTONE_BUILD_TESTS=OFF -DCAPSTONE_BUILD_CSTOOL=OFF -G "Unix Makefiles" ..') os.system("cmake --build .") @@ -154,10 +148,6 @@ def build_libraries(): os.system("CAPSTONE_BUILD_CORE_ONLY=yes bash ./make.sh") 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) diff --git a/nmake.bat b/nmake.bat deleted file mode 100644 index ec2496edf5a..00000000000 --- a/nmake.bat +++ /dev/null @@ -1,30 +0,0 @@ -:: Capstone disassembler engine (www.capstone-engine.org) -:: Build Capstone libs (capstone.dll & capstone.lib) on Windows with CMake & Nmake -:: By Nguyen Anh Quynh, Jorn Vernee, 2017, 2019 - -@echo off - -set flags="-DCMAKE_BUILD_TYPE=Release -DCAPSTONE_BUILD_STATIC_RUNTIME=ON" - -if "%1"=="ARM" set %arch%=ARM -if "%1"=="ARM64" set %arch%=ARM64 -if "%1"=="M68K" set %arch%=M68K -if "%1"=="MIPS" set %arch%=MIPS -if "%1"=="PowerPC" set %arch%=PPC -if "%1"=="Sparc" set %arch%=SPARC -if "%1"=="SystemZ" set %arch%=SYSZ -if "%1"=="XCore" set %arch%=XCORE -if "%1"=="x86" set %arch%=X86 -if "%1"=="TMS320C64x" set %arch%=TMS320C64X -if "%1"=="M680x" set %arch%=M680X -if "%1"=="EVM" set %arch%=EVM -if "%1"=="MOS65XX" set %arch%=MOS65XX -if "%1"=="WASM" set %arch%=WASM -if "%1"=="BPF" set %arch%=BPF -if "%1"=="RISCV" set %arch%=RISCV - -if not "%arch%"=="" set flags=%flags% and " -DCAPSTONE_ARCHITECTURE_DEFAULT=OFF -DCAPSTONE_%arch%_SUPPORT=ON" - -cmake %flags% -G "NMake Makefiles" .. -nmake -