@@ -184,12 +184,6 @@ function _get_cxxflags(package, opt)
184
184
table .join2 (result , _map_compflags (package , " cxx" , " includedir" , package :build_getenv (" includedirs" )))
185
185
table .join2 (result , _map_compflags (package , " cxx" , " sysincludedir" , package :build_getenv (" sysincludedirs" )))
186
186
end
187
- local runtimes = package :runtimes ()
188
- if runtimes then
189
- local fake_target = {is_shared = function (_ ) return false end ,
190
- sourcekinds = function (_ ) return " cxx" end }
191
- table .join2 (result , _map_compflags (fake_target , " cxx" , " runtime" , runtimes ))
192
- end
193
187
table .join2 (result , package :config (" cxxflags" ))
194
188
table .join2 (result , package :config (" cxflags" ))
195
189
if opt .cxxflags then
@@ -239,12 +233,6 @@ function _get_ldflags(package, opt)
239
233
table .join2 (result , _map_linkflags (package , " binary" , {" cxx" }, " syslink" , package :build_getenv (" syslinks" )))
240
234
table .join2 (result , _map_linkflags (package , " binary" , {" cxx" }, " linkdir" , package :build_getenv (" linkdirs" )))
241
235
end
242
- local runtimes = package :runtimes ()
243
- if runtimes then
244
- local fake_target = {is_shared = function (_ ) return false end ,
245
- sourcekinds = function (_ ) return " cxx" end }
246
- table .join2 (result , _map_linkflags (fake_target , " binary" , {" cxx" }, " runtime" , runtimes ))
247
- end
248
236
table .join2 (result , package :config (" ldflags" ))
249
237
if package :config (" lto" ) then
250
238
table .join2 (result , package :_generate_lto_configs ().ldflags )
@@ -271,12 +259,6 @@ function _get_shflags(package, opt)
271
259
table .join2 (result , _map_linkflags (package , " shared" , {" cxx" }, " syslink" , package :build_getenv (" syslinks" )))
272
260
table .join2 (result , _map_linkflags (package , " shared" , {" cxx" }, " linkdir" , package :build_getenv (" linkdirs" )))
273
261
end
274
- local runtimes = package :runtimes ()
275
- if runtimes then
276
- local fake_target = {is_shared = function (_ ) return true end ,
277
- sourcekinds = function (_ ) return " cxx" end }
278
- table .join2 (result , _map_linkflags (fake_target , " shared" , {" cxx" }, " runtime" , runtimes ))
279
- end
280
262
table .join2 (result , package :config (" shflags" ))
281
263
if package :config (" lto" ) then
282
264
table .join2 (result , package :_generate_lto_configs ().shflags )
@@ -401,28 +383,14 @@ function _get_configs_for_windows(package, configs, opt)
401
383
-- we maybe need patch `cmake_policy(SET CMP0091 NEW)` to enable this argument for some packages
402
384
-- @see https://cmake.org/cmake/help/latest/policy/CMP0091.html#policy:CMP0091
403
385
-- https://github.com/xmake-io/xmake-repo/pull/303
404
- local runtime
405
386
if package :has_runtime (" MT" ) then
406
387
table.insert (configs , " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded" )
407
- runtime = " MT"
408
388
elseif package :has_runtime (" MTd" ) then
409
389
table.insert (configs , " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebug" )
410
- runtime = " MTd"
411
390
elseif package :has_runtime (" MD" ) then
412
391
table.insert (configs , " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL" )
413
- runtime = " MD"
414
392
elseif package :has_runtime (" MDd" ) then
415
393
table.insert (configs , " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebugDLL" )
416
- runtime = " MDd"
417
- end
418
- if runtime then
419
- -- CMake default MSVC flags as of 3.21.2
420
- local default_debug_flags = " /Zi /Ob0 /Od /RTC1"
421
- local default_release_flags = " /O2 /Ob2 /DNDEBUG"
422
- table.insert (configs , ' -DCMAKE_CXX_FLAGS_DEBUG=/' .. runtime .. ' ' .. default_debug_flags )
423
- table.insert (configs , ' -DCMAKE_CXX_FLAGS_RELEASE=/' .. runtime .. ' ' .. default_release_flags )
424
- table.insert (configs , ' -DCMAKE_C_FLAGS_DEBUG=/' .. runtime .. ' ' .. default_debug_flags )
425
- table.insert (configs , ' -DCMAKE_C_FLAGS_RELEASE=/' .. runtime .. ' ' .. default_release_flags )
426
394
end
427
395
if not opt ._configs_str :find (" CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY" ) then
428
396
table.insert (configs , " -DCMAKE_COMPILE_PDB_OUTPUT_DIRECTORY=pdb" )
@@ -786,9 +754,103 @@ function _get_configs(package, configs, opt)
786
754
else
787
755
_get_configs_for_generic (package , configs , opt )
788
756
end
757
+ _get_configs_for_default_flags (package , configs , opt )
789
758
return configs
790
759
end
791
760
761
+ function _get_default_flags (package , configs , buildtype , opt )
762
+ local cmake_default_flags = _g .cmake_default_flags
763
+ if not cmake_default_flags then
764
+ local tmpdir = path .join (os .tmpdir (), package :name (), package :mode ())
765
+ local dummy_cmakelist = path .join (tmpdir , " CMakeLists.txt" )
766
+
767
+ io .writefile (dummy_cmakelist , format ([[
768
+ message("CMAKE_C_FLAGS is ${CMAKE_C_FLAGS}")
769
+ message("CMAKE_C_FLAGS_%s is ${CMAKE_C_FLAGS_%s}")
770
+
771
+ message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
772
+ message("CMAKE_CXX_FLAGS_%s is ${CMAKE_CXX_FLAGS_%s}")
773
+
774
+ message("CMAKE_EXE_LINKER_FLAGS is ${CMAKE_EXE_LINKER_FLAGS}")
775
+ message("CMAKE_EXE_LINKER_FLAGS_%s is ${CMAKE_EXE_LINKER_FLAGS_%s}")
776
+
777
+ message("CMAKE_SHARED_LINKER_FLAGS is ${CMAKE_SHARED_LINKER_FLAGS}")
778
+ message("CMAKE_SHARED_LINKER_FLAGS_%s is ${CMAKE_SHARED_LINKER_FLAGS_%s}")
779
+
780
+ message("CMAKE_STATIC_LINKER_FLAGS is ${CMAKE_STATIC_LINKER_FLAGS}")
781
+ message("CMAKE_STATIC_LINKER_FLAGS_%s is ${CMAKE_STATIC_LINKER_FLAGS_%s}")
782
+ ]] , buildtype , buildtype , buildtype , buildtype , buildtype , buildtype , buildtype , buildtype , buildtype , buildtype ))
783
+
784
+ local runenvs = opt .envs or buildenvs (package )
785
+ local cmake = find_tool (" cmake" , {envs = runenvs })
786
+ local _configs = table .join (configs , " -S " .. path .directory (dummy_cmakelist ), " -B " .. tmpdir )
787
+ local _ , outdata = os .iorunv (cmake .program , _configs , {envs = runenvs })
788
+ cmake_default_flags = {}
789
+ cmake_default_flags .cflags = outdata :match (" CMAKE_C_FLAGS is (.-)\n " ):split (" " )
790
+ table .join2 (cmake_default_flags .cflags , outdata :match (format (" CMAKE_C_FLAGS_%s is (.-)\n " , buildtype )):replace (" /MDd" , " " ):replace (" /MD" , " " ):split (" " ))
791
+ cmake_default_flags .cxxflags = outdata :match (" CMAKE_CXX_FLAGS is (.-)\n " ):split (" " )
792
+ table .join2 (cmake_default_flags .cxxflags , outdata :match (format (" CMAKE_CXX_FLAGS_%s is (.-)\n " , buildtype )):replace (" /MDd" , " " ):replace (" /MD" , " " ):split (" " ))
793
+ cmake_default_flags .ldflags = outdata :match (" CMAKE_EXE_LINKER_FLAGS is (.-)\n " ):split (" " )
794
+ table .join2 (cmake_default_flags .ldflags , outdata :match (format (" CMAKE_EXE_LINKER_FLAGS_%s is (.-)\n " , buildtype )):split (" " ))
795
+ cmake_default_flags .shflags = outdata :match (" CMAKE_SHARED_LINKER_FLAGS is (.-)\n " ):split (" " )
796
+ table .join2 (cmake_default_flags .shflags , outdata :match (format (" CMAKE_SHARED_LINKER_FLAGS_%s is (.-)\n " , buildtype )):split (" " ))
797
+ cmake_default_flags .arflags = outdata :match (" CMAKE_STATIC_LINKER_FLAGS is (.-)\n " ):split (" " )
798
+ table .join2 (cmake_default_flags .arflags , outdata :match (format (" CMAKE_STATIC_LINKER_FLAGS_%s is (.-)\n " , buildtype )):split (" " ))
799
+
800
+ cmake_default_flags .cflags = table .unique (cmake_default_flags .cflags )
801
+ cmake_default_flags .cxxflags = table .unique (cmake_default_flags .cxxflags )
802
+ cmake_default_flags .ldflags = table .unique (cmake_default_flags .ldflags )
803
+ cmake_default_flags .shflags = table .unique (cmake_default_flags .shflags )
804
+ cmake_default_flags .arflags = table .unique (cmake_default_flags .arflags )
805
+
806
+ _g .cmake_default_flags = cmake_default_flags
807
+ end
808
+ return cmake_default_flags
809
+ end
810
+
811
+ function _get_configs_for_default_flags (package , configs , opt )
812
+ local cmake_buildtype_map = {
813
+ debug = " DEBUG" ,
814
+ release = " RELEASE" ,
815
+ releasedbg = " RELWITHDEBINFO"
816
+ }
817
+ local buildtype = package :mode ()
818
+ if not buildtype :startswith (" release" ) and buildtype ~= " debug" then
819
+ buildtype = " release"
820
+ end
821
+ buildtype = cmake_buildtype_map [buildtype ]
822
+
823
+ local runtimes = package :runtimes ()
824
+ local cxx_runtimeflags
825
+ local c_runtimeflags
826
+ local ld_runtimeflags
827
+ local sh_runtimeflags
828
+ local ar_runtimeflags
829
+ if runtimes then
830
+ local fake_target = {is_shared = function (_ ) return false end ,
831
+ sourcekinds = function (_ ) return " cxx" end }
832
+ c_runtimeflags = _map_compflags (fake_target , " c" , " runtime" , runtimes )
833
+ fake_target .sourcekinds = function (_ ) return " cxx" end
834
+ cxx_runtimeflags = _map_compflags (fake_target , " cxx" , " runtime" , runtimes )
835
+ ld_runtimeflags = _map_linkflags (fake_target , " binary" , {" cxx" }, " runtime" , runtimes )
836
+ ar_runtimeflags = _map_linkflags (fake_target , " static" , {" cxx" }, " runtime" , runtimes )
837
+ fake_target .is_shared = function (_ ) return true end
838
+ sh_runtimeflags = _map_linkflags (fake_target , " shared" , {" cxx" }, " runtime" , runtimes )
839
+ end
840
+ local default_flags = _get_default_flags (package , configs , buildtype , opt )
841
+ local cxx_init_flags = (opt .cxxflags or opt .cxflags ) and (cxx_runtimeflags or {}) or table .join (default_flags .cxxflags , cxx_runtimeflags or {})
842
+ local c_init_flags = (opt .cflags or opt .cxflags ) and (c_runtimeflags or {}) or table .join (default_flags .cflags , c_runtimeflags or {})
843
+ local ld_init_flags = (opt .ldflags ) and (ld_runtimeflags or {}) or table .join (default_flags .ldflags , ld_runtimeflags or {})
844
+ local ar_init_flags = (opt .arflags ) and (ar_runtimeflags or {}) or table .join (default_flags .arflags , ar_runtimeflags or {})
845
+ local sh_init_flags = (opt .shflags ) and (sh_runtimeflags or {}) or table .join (default_flags .shflags , sh_runtimeflags or {})
846
+
847
+ table.insert (configs , format (" -DCMAKE_CXX_FLAGS_%s=" , buildtype ) .. table.concat (cxx_init_flags , " " ))
848
+ table.insert (configs , format (" -DCMAKE_C_FLAGS_%s=" , buildtype ) .. table.concat (c_init_flags , " " ))
849
+ table.insert (configs , format (" -DCMAKE_EXE_LINKER_FLAGS_%s=" , buildtype ) .. table.concat (ld_init_flags , " " ))
850
+ table.insert (configs , format (" -DCMAKE_STATIC_LINKER_FLAGS_%s=" , buildtype ) .. table.concat (ar_init_flags , " " ))
851
+ table.insert (configs , format (" -DCMAKE_SHARED_LINKER_FLAGS_%s=" , buildtype ) .. table.concat (sh_init_flags , " " ))
852
+ end
853
+
792
854
-- get build environments
793
855
function buildenvs (package , opt )
794
856
0 commit comments