@@ -5,9 +5,19 @@ project(binlog_json_parser)
55set (CMAKE_CXX_STANDARD 23)
66set (CMAKE_CXX_STANDARD_REQUIRED ON )
77
8+ include (CheckIPOSupported)
9+ include (CheckCSourceCompiles)
10+ check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_OUTPUT)
11+
12+ if (IPO_SUPPORTED)
13+ set (CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE )
14+ message (STATUS "Interprocedural optimization (IPO/LTO) enabled globally." )
15+ else ()
16+ message (STATUS "IPO/LTO is not supported: ${IPO_OUTPUT} " )
17+ endif ()
18+
819# Check if the build type is Release
920if (CMAKE_BUILD_TYPE STREQUAL "Release" )
10-
1121 # Set optimization level to -O3 for release builds
1222 if (NOT CMAKE_CXX_FLAGS_RELEASE MATCHES "-O" )
1323 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3" )
@@ -28,28 +38,47 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
2838 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=skylake" )
2939 endif ()
3040 endif ()
41+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|ARM64" )
42+ if (USE_MARCH_NATIVE)
43+ set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native" )
44+ else ()
45+ if (NOT CMAKE_CXX_FLAGS_RELEASE MATCHES "march=" )
46+ set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=armv8.2-a+dotprod+fp16+rcpc+lse+simd+sve" )
47+ endif ()
48+ endif ()
3149 else ()
32- message (WARNING "The -march option will not be set because the system is not x86 or x64." )
33- endif ()
34-
35- # Check for LTO support
36- include (CheckCXXCompilerFlag)
37-
38- check_cxx_compiler_flag("-flto" COMPILER_SUPPORTS_LTO)
39-
40- if (COMPILER_SUPPORTS_LTO)
41- message (STATUS "Link Time Optimization (LTO) is supported by the compiler." )
42- set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto" )
43- set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto" )
44- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto" )
45- else ()
46- message (WARNING "Link Time Optimization (LTO) is not supported by the compiler." )
50+ message (WARNING "The -march option will not be set because the system is not x86, x64, or ARM64." )
4751 endif ()
4852
4953 # Export compile flags to a file
5054 file (WRITE "${CMAKE_BINARY_DIR} /compile_flags.txt" "CXXFLAGS: ${CMAKE_CXX_FLAGS_RELEASE} \n " )
5155 file (APPEND "${CMAKE_BINARY_DIR} /compile_flags.txt" "LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS} \n " )
52-
5356endif ()
5457
55- add_library (mysqljsonparse SHARED mysqljsonparse.cpp mysql_json_parser.cpp)
58+ check_c_source_compiles("
59+ #include <features.h>
60+ #if defined(__GLIBC__)
61+ #error \" This is glibc, not musl\"
62+ #endif
63+ #include <stdio.h>
64+ int main() { return 0; }
65+ " IS_MUSL)
66+
67+ option (ALPINE_STATIC "Force fully static build when using musl/Alpine" ${IS_MUSL} )
68+
69+ if (ALPINE_STATIC)
70+ add_definitions (-D_FORTIFY_SOURCE=0)
71+ message (STATUS "musl detected → producing shared library with static musl linking" )
72+
73+ add_library (mysqljsonparse SHARED mysqljsonparse.cpp mysql_json_parser.cpp)
74+ target_link_options (mysqljsonparse PRIVATE
75+ -static
76+ -static -libgcc
77+ -static -libstdc++
78+ -fPIC
79+ )
80+ target_compile_options (mysqljsonparse PRIVATE -fPIC)
81+ else ()
82+ message (STATUS "musl not detected → building shared library with dynamic glibc" )
83+ add_library (mysqljsonparse SHARED mysqljsonparse.cpp mysql_json_parser.cpp)
84+ endif ()
0 commit comments