@@ -355,19 +355,25 @@ function _add_project(cmakelists, outputdir)
355355 if _can_native_support_for_cxxmodules () then
356356 cmakelists :print (" set(CMAKE_CXX_SCAN_FOR_MODULES ON)" )
357357 end
358+ local languages = _get_project_languages ()
358359 if project_name then
359360 local project_info = " "
360361 local project_version = project .version ()
361362 if project_version then
362363 project_info = project_info .. " VERSION " .. project_version
363364 end
364- local languages = _get_project_languages ()
365365 if languages then
366366 cmakelists :print (" project(%s%s LANGUAGES %s)" , project_name , project_info , table.concat (languages , " " ))
367367 else
368368 cmakelists :print (" project(%s%s)" , project_name , project_info )
369369 end
370370 end
371+ -- Define a language-independant global compiler_id variable
372+ if (languages and # languages > 0 ) then
373+ cmakelists :print (" set(CURRENT_COMPILER_ID ${CMAKE_%s_COMPILER_ID})" , _get_project_languages ()[1 ])
374+ else
375+ cmakelists :print (" set(CURRENT_COMPILER_ID ${CMAKE_C_COMPILER_ID})" ) -- C should be defined by default if not specified
376+ end
371377 cmakelists :print (" " )
372378end
373379
@@ -784,7 +790,7 @@ function _add_target_compile_options(cmakelists, target, outputdir)
784790 for _ , toolname in toolnames :keys () do
785791 local name = compilernames [toolname ]
786792 if name then
787- cmakelists :print (" if(CMAKE_COMPILER_ID STREQUAL \" %s\" )" , name )
793+ cmakelists :print (" if(CURRENT_COMPILER_ID STREQUAL \" %s\" )" , name )
788794 _add_target_compile_options_for_compiler (toolname )
789795 cmakelists :print (" endif()" )
790796 end
@@ -811,17 +817,17 @@ function _add_target_values(cmakelists, target, name)
811817 if name :endswith (" s" ) then
812818 name = name :sub (1 , # name - 1 )
813819 end
814- cmakelists :print (" if(CMAKE_COMPILER_ID STREQUAL \" MSVC\" )" )
820+ cmakelists :print (" if(CURRENT_COMPILER_ID STREQUAL \" MSVC\" )" )
815821 local flags_cl = _map_compflags (" cl" , " c" , name , values )
816822 for _ , flag in ipairs (flags_cl ) do
817823 cmakelists :print (" target_compile_options(%s PRIVATE %s)" , target :name (), flag )
818824 end
819- cmakelists :print (" elseif(CMAKE_COMPILER_ID STREQUAL \" Clang\" )" )
825+ cmakelists :print (" elseif(CURRENT_COMPILER_ID STREQUAL \" Clang\" )" )
820826 local flags_clang = _map_compflags (" clang" , " c" , name , values )
821827 for _ , flag in ipairs (flags_clang ) do
822828 cmakelists :print (" target_compile_options(%s PRIVATE %s)" , target :name (), flag )
823829 end
824- cmakelists :print (" elseif(CMAKE_COMPILER_ID STREQUAL \" GNU\" )" )
830+ cmakelists :print (" elseif(CURRENT_COMPILER_ID STREQUAL \" GNU\" )" )
825831 local flags_gcc = _map_compflags (" gcc" , " c" , name , values )
826832 for _ , flag in ipairs (flags_gcc ) do
827833 cmakelists :print (" target_compile_options(%s PRIVATE %s)" , target :name (), flag )
@@ -888,7 +894,7 @@ function _add_target_languages(cmakelists, target)
888894 if flag :endswith (' ++' ) then
889895 cmakelists :print (' foreach(standard 26 23 20 17 14 11 98)' )
890896 cmakelists :print (' include(CheckCXXCompilerFlag)' )
891- cmakelists :print (' if(CMAKE_COMPILER_ID STREQUAL \" MSVC\" )' )
897+ cmakelists :print (' if(CURRENT_COMPILER_ID STREQUAL \" MSVC\" )' )
892898 cmakelists :print (' check_cxx_compiler_flag("/std:%s${standard}" %s_support_%s_standard_${standard})' , flag , target :name (), flag )
893899 cmakelists :print (' else()' )
894900 cmakelists :print (' check_cxx_compiler_flag("-std=%s${standard}" %s_support_%s_standard_${standard})' , flag , target :name (), flag )
@@ -901,7 +907,7 @@ function _add_target_languages(cmakelists, target)
901907 else
902908 cmakelists :print (' foreach(standard 23 17 11 99 90)' )
903909 cmakelists :print (' include(CheckCCompilerFlag)' )
904- cmakelists :print (' if(CMAKE_COMPILER_ID STREQUAL \" MSVC\" )' )
910+ cmakelists :print (' if(CURRENT_COMPILER_ID STREQUAL \" MSVC\" )' )
905911 cmakelists :print (' check_c_compiler_flag("/std:%s${standard}" %s_support_%s_standard_${standard})' , flag , target :name (), flag )
906912 cmakelists :print (' else()' )
907913 cmakelists :print (' check_c_compiler_flag("-std=%s${standard}" %s_support_%s_standard_${standard})' , flag , target :name (), flag )
@@ -941,7 +947,7 @@ function _add_target_optimization(cmakelists, target)
941947 }
942948 local optimization = target :get (" optimize" )
943949 if optimization then
944- cmakelists :print (" if(CMAKE_COMPILER_ID STREQUAL \" MSVC\" )" )
950+ cmakelists :print (" if(CURRENT_COMPILER_ID STREQUAL \" MSVC\" )" )
945951 cmakelists :print (" target_compile_options(%s PRIVATE %s)" , target :name (), flags_msvc [optimization ])
946952 cmakelists :print (" else()" )
947953 cmakelists :print (" target_compile_options(%s PRIVATE %s)" , target :name (), flags_gcc [optimization ])
@@ -969,7 +975,7 @@ function _add_target_symbols(cmakelists, target)
969975 if levels :has (" hidden" ) then
970976 table.insert (flags_gcc , " -fvisibility=hidden" )
971977 end
972- cmakelists :print (" if(CMAKE_COMPILER_ID STREQUAL \" MSVC\" )" )
978+ cmakelists :print (" if(CURRENT_COMPILER_ID STREQUAL \" MSVC\" )" )
973979 if # flags_msvc > 0 then
974980 cmakelists :print (" target_compile_options(%s PRIVATE %s)" , target :name (), table.concat (flags_msvc , " " ))
975981 end
@@ -990,7 +996,7 @@ function _add_target_runtimes(cmakelists, target)
990996 local cmake_minver = _get_cmake_minver ()
991997 if cmake_minver :ge (" 3.15.0" ) then
992998 local runtimes = target :get (" runtimes" )
993- cmakelists :print (" if(CMAKE_COMPILER_ID STREQUAL \" MSVC\" )" )
999+ cmakelists :print (" if(CURRENT_COMPILER_ID STREQUAL \" MSVC\" )" )
9941000 if runtimes then
9951001 if runtimes == " MT" then
9961002 runtimes = " MultiThreaded"
@@ -1105,7 +1111,7 @@ function _add_target_link_directories(cmakelists, target, outputdir)
11051111 end
11061112 cmakelists :print (" )" )
11071113 else
1108- cmakelists :print (" if(CMAKE_COMPILER_ID STREQUAL \" MSVC\" )" )
1114+ cmakelists :print (" if(CURRENT_COMPILER_ID STREQUAL \" MSVC\" )" )
11091115 cmakelists :print (" target_link_libraries(%s PRIVATE" , target :name ())
11101116 for _ , linkdir in ipairs (linkdirs ) do
11111117 cmakelists :print (" -libpath:" .. _get_relative_unix_path (linkdir , outputdir ))
@@ -1172,7 +1178,7 @@ function _add_target_link_options(cmakelists, target, outputdir)
11721178 for _ , toolname in toolnames :keys () do
11731179 local name = linkernames [toolname ]
11741180 if name then
1175- cmakelists :print (" if(CMAKE_COMPILER_ID STREQUAL \" %s\" )" , name )
1181+ cmakelists :print (" if(CURRENT_COMPILER_ID STREQUAL \" %s\" )" , name )
11761182 _add_target_link_options_for_linker (toolname )
11771183 cmakelists :print (" endif()" )
11781184 end
0 commit comments