@@ -20,38 +20,162 @@ set(CMAKE_C_STANDARD 11)
2020#SET_OPTION(Plugin.SymbolResolver ON)
2121#add_subdirectory(../libs/Dobby dobby)
2222
23- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=return-type -Wno-invalid-offsetof" )
24- if ("${CMAKE_CXX_COMPILER_ID} " MATCHES "Clang" )
25- SET (CLANG_CXX_EXTRA_OPT "-Werror=unknown-warning-option -Werror=format-invalid-specifier -Werror=call-to-pure-virtual-from-ctor-dtor" )
26- SET (CLANG_C_EXTRA_OPT "-Werror=format-invalid-specifier" )
23+ # =============================================================================
24+ # Cross-Platform Compiler Configuration
25+ # =============================================================================
26+
27+ # Detect compiler and set appropriate flags
28+ if (MSVC )
29+ # Microsoft Visual C++ Compiler
30+ message (STATUS "Configuring for MSVC compiler" )
31+
32+ # Warning configuration for MSVC
33+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4" ) # High warning level
34+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /we4716" ) # Error on missing return (equivalent to -Werror=return-type)
35+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996" ) # Disable deprecated warnings (similar to -Wno-invalid-offsetof)
36+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4068" ) # Disable unknown pragma warnings
37+
38+ # Frame pointer configuration
39+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Oy-" ) # Keep frame pointers (equivalent to -fno-omit-frame-pointer)
40+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Oy-" )
41+
42+ # Runtime library configuration
43+ if (CMAKE_BUILD_TYPE MATCHES Debug)
44+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MTd" ) # Static debug runtime
45+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MTd" )
46+ else ()
47+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT" ) # Static release runtime
48+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MT" )
49+ endif ()
50+
51+ # Optimization flags for release builds
52+ if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
53+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2" ) # Optimize for speed
54+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL" ) # Whole program optimization
55+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LTCG" ) # Link-time code generation
56+ endif ()
57+
58+ # MSVC-specific definitions
59+ add_definitions (-D_CRT_SECURE_NO_WARNINGS) # Disable CRT security warnings
60+ add_definitions (-DNOMINMAX) # Prevent Windows.h from defining min/max macros
61+ add_definitions (-DWIN32_LEAN_AND_MEAN) # Reduce Windows header bloat
62+
63+ # Symbol visibility (MSVC uses __declspec instead of -fvisibility)
64+ # We'll handle exports manually to avoid auto-generation issues
65+ set (CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF )
66+
67+ elseif ("${CMAKE_CXX_COMPILER_ID} " MATCHES "Clang" )
68+ # Clang Compiler
69+ message (STATUS "Configuring for Clang compiler" )
70+
71+ # Clang-specific warning flags
72+ set (CLANG_CXX_EXTRA_OPT "-Werror=unknown-warning-option -Werror=format-invalid-specifier -Werror=call-to-pure-virtual-from-ctor-dtor" )
73+ set (CLANG_C_EXTRA_OPT "-Werror=format-invalid-specifier" )
74+
75+ # Base warning and error flags
76+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=return-type -Wno-invalid-offsetof" )
77+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_CXX_EXTRA_OPT} " )
78+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CLANG_C_EXTRA_OPT} " )
79+
80+ # Position Independent Code (not needed on Windows)
81+ if (NOT WIN32 )
82+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" )
83+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC" )
84+ endif ()
85+
86+ # Frame pointer configuration
87+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer" )
88+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer" )
89+
90+ # Virtual destructor warnings
91+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=non-virtual-dtor -Werror=delete-non-virtual-dtor" )
92+
93+ # Symbol visibility
94+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=protected" )
95+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=protected" )
96+ if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
97+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden" )
98+ endif ()
99+
100+ # Static linking of standard libraries (Unix/Linux only)
101+ if (UNIX AND NOT APPLE )
102+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++ -static-libgcc" )
103+ endif ()
104+
105+ elseif ("${CMAKE_CXX_COMPILER_ID} " MATCHES "GNU" )
106+ # GCC Compiler
107+ message (STATUS "Configuring for GCC compiler" )
108+
109+ # Base warning and error flags
110+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=return-type -Wno-invalid-offsetof" )
111+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=return-type" )
112+
113+ # Position Independent Code (not needed on Windows)
114+ if (NOT WIN32 )
115+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" )
116+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC" )
117+ endif ()
118+
119+ # Frame pointer configuration
120+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer" )
121+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer" )
122+
123+ # Virtual destructor warnings
124+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=non-virtual-dtor -Werror=delete-non-virtual-dtor" )
125+
126+ # Symbol visibility
127+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=protected" )
128+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=protected" )
129+ if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
130+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden" )
131+ endif ()
132+
133+ # Static linking of standard libraries (Unix/Linux only)
134+ if (UNIX AND NOT APPLE )
135+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++ -static-libgcc" )
136+ endif ()
137+
27138else ()
28- SET (CLANG_CXX_EXTRA_OPT "" )
29- SET (CLANG_C_EXTRA_OPT "" )
139+ # Unknown compiler - use minimal configuration
140+ message (WARNING "Unknown compiler: ${CMAKE_CXX_COMPILER_ID} . Using minimal configuration." )
141+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer" )
142+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer" )
30143endif ()
31144
32- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_CXX_EXTRA_OPT} -fPIC -Werror=return-type -Wno-invalid-offsetof" )
33- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CLANG_C_EXTRA_OPT} -fPIC -Werror=return-type" )
145+ # =============================================================================
146+ # Platform-Specific Linker Configuration
147+ # =============================================================================
34148
35- # default visibility protected, but for release build, inline hidden for c++ code to reduce binary size
36- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=protected" )
37- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=protected" )
38- if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
39- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden" )
40- endif ()
149+ if (UNIX AND NOT APPLE )
150+ # Linux-specific linker flags
151+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,max-page-size=16384" )
152+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-allow-shlib-undefined,--no-undefined" )
153+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs,-z,now,-z,relro" )
41154
42- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,max-page-size=16384 -Wl,--no-allow-shlib-undefined,--no-undefined -Wl,-z,defs,-z,now,-z,relro" )
155+ # Garbage collection for release builds
156+ if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
157+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections" )
158+ endif ()
43159
44- # if we are releasing, ask linker to reduce the size of the binary, eg. remove unused code
45- if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
46- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections" )
47- endif ()
160+ elseif (APPLE )
161+ # macOS-specific linker flags
162+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-undefined,error" )
48163
49- # add -fno-omit-frame-pointer -Werror=non-virtual-dtor -Werror=delete-non-virtual-dtor
50- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -Werror=non-virtual-dtor -Werror=delete-non-virtual-dtor" )
51- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer" )
164+ # Garbage collection for release builds
165+ if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
166+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-dead_strip" )
167+ endif ()
52168
53- # prefer static linking libstdc++ and libgcc
54- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++ -static-libgcc" )
169+ elseif (WIN32 )
170+ # Windows-specific linker flags
171+ if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
172+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /OPT:REF /OPT:ICF" )
173+ endif ()
174+ endif ()
175+
176+ # =============================================================================
177+ # External Dependencies
178+ # =============================================================================
55179
56180add_subdirectory (../libs/fmt fmt)
57181
@@ -62,6 +186,10 @@ target_compile_definitions(fmt-header-only INTERFACE FMT_STATIC_THOUSANDS_SEPARA
62186# target_compile_options(dobby PRIVATE -fomit-frame-pointer)
63187#endif ()
64188
189+ # =============================================================================
190+ # Target Definition
191+ # =============================================================================
192+
65193add_library (
66194 jvmplant
67195 SHARED
@@ -83,5 +211,65 @@ target_link_libraries(
83211 jvmplant
84212 PRIVATE
85213 fmt-header-only
86- # dobby
214+ # dobby
87215)
216+
217+ # =============================================================================
218+ # Platform-Specific Library Dependencies
219+ # =============================================================================
220+
221+ if (WIN32 )
222+ # Windows-specific libraries
223+ target_link_libraries (jvmplant PRIVATE
224+ kernel32
225+ user32
226+ advapi32
227+ shell32
228+ )
229+ elseif (UNIX )
230+ # Unix-specific libraries
231+ target_link_libraries (jvmplant PRIVATE
232+ ${CMAKE_DL_LIBS} # Dynamic loading library (libdl)
233+ pthread # POSIX threads
234+ )
235+ endif ()
236+
237+ # =============================================================================
238+ # Compiler-Specific Target Properties
239+ # =============================================================================
240+
241+ if (MSVC )
242+ # MSVC-specific target properties
243+ set_target_properties (jvmplant PROPERTIES
244+ COMPILE_PDB_NAME "jvmplant"
245+ COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR} "
246+ # Disable automatic DLL export generation that's causing issues
247+ WINDOWS_EXPORT_ALL_SYMBOLS OFF
248+ )
249+
250+ # Generate debug information for release builds
251+ if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
252+ set_target_properties (jvmplant PROPERTIES
253+ LINK_FLAGS "/DEBUG /PDBALTPATH:%_PDB%"
254+ )
255+ endif ()
256+ endif ()
257+
258+ # Set output directory for all configurations
259+ set_target_properties (jvmplant PROPERTIES
260+ RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR} /bin"
261+ LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR} /lib"
262+ ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR} /lib"
263+ )
264+
265+ # =============================================================================
266+ # Build Information
267+ # =============================================================================
268+
269+ message (STATUS "Build configuration:" )
270+ message (STATUS " Compiler: ${CMAKE_CXX_COMPILER_ID} " )
271+ message (STATUS " Build type: ${CMAKE_BUILD_TYPE} " )
272+ message (STATUS " C++ Standard: ${CMAKE_CXX_STANDARD} " )
273+ message (STATUS " C Standard: ${CMAKE_C_STANDARD} " )
274+ message (STATUS " Platform: ${CMAKE_SYSTEM_NAME} " )
275+ message (STATUS " Architecture: ${CMAKE_SYSTEM_PROCESSOR} " )
0 commit comments