forked from Enhex/premake-cmake
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcmake_project.lua
More file actions
executable file
·1602 lines (1503 loc) · 52.4 KB
/
Copy pathcmake_project.lua
File metadata and controls
executable file
·1602 lines (1503 loc) · 52.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Name: cmake_project.lua
-- Purpose: Generate a cmake C/C++ project file.
-- Author: Ryan Pusztai
-- Modified by: Andrea Zanellato
-- Manu Evans
-- Tom van Dijck
-- Yehonatan Ballas
-- Joel Linn
-- UndefinedVertex
-- Joris Dauphin
-- alchemyyy
-- Created: 2013/05/06
-- Copyright: (c) 2008-2024 Jason Perkins and the Premake project
--
local p = premake
local tree = p.tree
local project = p.project
local config = p.config
local cmake = p.modules.cmake
cmake.project = {}
local m = cmake.project
-- CMake-native command token translations using ${CMAKE_COMMAND} -E
-- This replaces the platform-specific translations (copy, cp, xcopy, etc.)
-- with cross-platform CMake commands that work on all platforms.
local cmake_command_tokens = {
chdir = function(v)
return "${CMAKE_COMMAND} -E chdir " .. path.normalize(v)
end,
copy = function(v)
return "${CMAKE_COMMAND} -E copy_directory " .. path.normalize(v)
end,
copyfile = function(v)
return "${CMAKE_COMMAND} -E copy " .. path.normalize(v)
end,
copyfileifnewer = function(v)
return "${CMAKE_COMMAND} -E copy_if_different " .. path.normalize(v)
end,
copydir = function(v)
return "${CMAKE_COMMAND} -E copy_directory " .. path.normalize(v)
end,
delete = function(v)
return "${CMAKE_COMMAND} -E rm -f " .. path.normalize(v)
end,
echo = function(v)
return "${CMAKE_COMMAND} -E echo " .. v
end,
linkdir = function(v)
return "${CMAKE_COMMAND} -E create_symlink " .. path.normalize(v)
end,
linkfile = function(v)
return "${CMAKE_COMMAND} -E create_symlink " .. path.normalize(v)
end,
mkdir = function(v)
return "${CMAKE_COMMAND} -E make_directory " .. path.normalize(v)
end,
move = function(v)
return "${CMAKE_COMMAND} -E rename " .. path.normalize(v)
end,
rmdir = function(v)
return "${CMAKE_COMMAND} -E rm -rf " .. path.normalize(v)
end,
touch = function(v)
return "${CMAKE_COMMAND} -E touch " .. path.normalize(v)
end,
}
function m.esc(s)
if type(s) == "table" then
return table.translate(s, m.esc)
end
s, _ = s:gsub('\\', '\\\\')
s, _ = s:gsub('"', '\\"')
return s
end
function m.unquote(s)
if type(s) == "table" then
return table.translate(s, m.unquote)
end
s, _ = s:gsub('"', '')
return s
end
function m.quote(s) -- handle single quote: required for "old" version of cmake
s, _ = premake.quote(s):gsub("'", " ")
return s
end
function m.getcompiler(cfg)
local default = iif(cfg.system == p.WINDOWS, "msc", "clang")
local toolset, toolset_version = p.tools.canonical(_OPTIONS.cc or cfg.toolset or default)
if not toolset then
error("Invalid toolset '" + (_OPTIONS.cc or cfg.toolset) + "'")
end
return toolset
end
function m.files(cfg)
local prj = cfg.project
local files = {}
table.foreachi(prj._.files, function(node)
if node.excludefrombuild then
return
end
local filecfg = p.fileconfig.getconfig(node, cfg)
local rule = p.global.getRuleForFile(node.name, prj.rules)
if p.fileconfig.hasFileSettings(filecfg) then
if filecfg.compilebuildoutputs then
for _, output in ipairs(filecfg.buildoutputs) do
table.insert(files, string.format('%s', path.getrelative(prj.workspace.location, output)))
end
end
elseif rule then
local environ = table.shallowcopy(filecfg.environ)
if rule.propertydefinition then
p.rule.prepareEnvironment(rule, environ, cfg)
p.rule.prepareEnvironment(rule, environ, filecfg)
end
local rulecfg = p.context.extent(rule, environ)
for _, output in ipairs(rulecfg.buildoutputs) do
table.insert(files, string.format('%s', path.getrelative(prj.workspace.location, output)))
end
elseif not node.generated then
table.insert(files, string.format('%s', path.getrelative(prj.workspace.location, node.abspath)))
end
end)
return files
end
local function is_empty(t)
for _, _ in pairs(t) do
return false
end
return true
end
local one_expression = "one_expression"
local table_expression = "table_expression"
local function generator_expression(prj, callback, mode)
local common = nil
local by_cfg = {}
for cfg in project.eachconfig(prj) do
local settings = callback(cfg)
if not common then
common = table.arraycopy(settings)
else
common = table.intersect(common, settings)
end
by_cfg[cfg] = settings
end
for cfg in project.eachconfig(prj) do
by_cfg[cfg] = table.difference(by_cfg[cfg], common)
if is_empty(by_cfg[cfg]) then
by_cfg[cfg] = nil
end
end
common_str = table.implode(common or {}, "", "", " ")
if is_empty(by_cfg) then
if mode == table_expression then
return common, true
else
return common_str, true
end
end
if #common_str > 0 then
common_str = common_str .. " "
end
if mode == one_expression then
local res = ''
local suffix = ''
for cfg, settings in pairs(by_cfg) do
res = res .. string.format('$<IF:$<CONFIG:%s>,%s,', cmake.cfgname(cfg), m.esc(common_str .. table.implode(settings, "", "", " ")))
suffix = suffix .. '>'
end
return res .. suffix, false
else
local res = {}
for cfg, settings in pairs(by_cfg) do
res = table.join(res, table.translate(settings, function(setting) return string.format('$<$<CONFIG:%s>:%s>', cmake.cfgname(cfg), m.esc(setting)) end))
end
if mode == table_expression then
return table.join(common, res), false
else
return common_str .. table.implode(res, "", "", " "), false
end
end
end
local function generate_prebuild(prj)
local prebuildcommands, same_output_by_cfg = generator_expression(prj, function(cfg)
local res = {}
if cfg.prebuildmessage or #cfg.prebuildcommands > 0 then
if cfg.prebuildmessage then
table.insert(res, os.translateCommandsAndPaths("{ECHO} " .. m.quote(cfg.prebuildmessage), cfg.project.basedir, cfg.project.location, cmake_command_tokens))
end
res = table.join(res, os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location, cmake_command_tokens))
end
return res
end, table_expression)
if #prebuildcommands == 0 then
return
end
local commands = {}
if not same_output_by_cfg then
for i, command in ipairs(prebuildcommands) do
local variable_name = string.format("PREBUILD_COMMAND_%s_%i", prj.name, i)
_p(0, 'SET(%s %s)', variable_name, command)
commands[i] = '"${' .. variable_name .. '}"'
end
else
commands = prebuildcommands
end
-- add_custom_command PRE_BUILD runs just before generating the target
-- so instead, use add_custom_target to run it before any rule (as obj)
_p(0, 'add_custom_target(prebuild-%s', prj.name)
for _, command in ipairs(commands) do
_p(1, 'COMMAND %s', command)
end
if not same_output_by_cfg then
_p(1, 'COMMAND_EXPAND_LISTS')
end
_p(0, ')')
_p(0, 'add_dependencies(%s prebuild-%s)', prj.name, prj.name)
end
local function generate_prelink(prj)
local prelinkcommands, same_output_by_cfg = generator_expression(prj, function(cfg)
local res = {}
if cfg.prelinkmessage or #cfg.prelinkcommands > 0 then
if cfg.prelinkmessage then
table.insert(res, os.translateCommandsAndPaths("{ECHO} " .. m.quote(cfg.prelinkmessage), cfg.project.basedir, cfg.project.location, cmake_command_tokens))
end
res = table.join(res, os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location, cmake_command_tokens))
end
return res
end, table_expression)
if #prelinkcommands == 0 then
return
end
local commands = {}
if not same_output_by_cfg then
for i, command in ipairs(prelinkcommands) do
local variable_name = string.format("PRELINK_COMMAND_%s_%i", prj.name, i)
_p(0, 'SET(%s %s)', variable_name, command)
commands[i] = '"${' .. variable_name .. '}"'
end
else
commands = prelinkcommands
end
_p(0, 'add_custom_command(TARGET %s PRE_LINK', prj.name)
for _, command in ipairs(commands) do
_p(1, 'COMMAND %s', command)
end
if not same_output_by_cfg then
_p(1, 'COMMAND_EXPAND_LISTS')
end
_p(0, ')')
end
local function generate_postbuild(prj)
local postbuildcommands, same_output_by_cfg = generator_expression(prj, function(cfg)
local res = {}
if cfg.postbuildmessage or #cfg.postbuildcommands > 0 then
if cfg.postbuildmessage then
table.insert(res, os.translateCommandsAndPaths("{ECHO} " .. m.quote(cfg.postbuildmessage), cfg.project.basedir, cfg.project.location, cmake_command_tokens))
end
res = table.join(res, os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location, cmake_command_tokens))
end
return res
end, table_expression)
if #postbuildcommands == 0 then
return
end
local commands = {}
if not same_output_by_cfg then
for i, command in ipairs(postbuildcommands) do
local variable_name = string.format("POSTBUILD_COMMAND_%i_%s", i, prj.name)
_p(0, 'SET(%s %s)', variable_name, command)
commands[i] = '"${' .. variable_name .. '}"'
end
else
commands = postbuildcommands
end
_p(0, 'add_custom_command(TARGET %s POST_BUILD', prj.name)
for _, command in ipairs(commands) do
_p(1, 'COMMAND %s', command)
end
if not same_output_by_cfg then
_p(1, 'COMMAND_EXPAND_LISTS')
end
_p(0, ')')
end
--
-- Project: Generate the cmake project file.
--
function m.generate(prj)
p.utf8()
if prj.kind == 'Utility' or prj.kind == 'None' then
-- Generate a custom target for Utility/None projects
local files = generator_expression(prj, m.files, table_expression)
_p('add_custom_target("%s" SOURCES', prj.name)
for _, file in ipairs(files) do
_p(1, '%s', file)
end
_p(')')
-- prebuild/postbuild still apply
generate_prebuild(prj)
generate_postbuild(prj)
return
end
if prj.kind == 'SharedItems' then
-- SharedItems are header-only / interface libraries
local files = generator_expression(prj, m.files, table_expression)
_p('add_library("%s" INTERFACE', prj.name)
for _, file in ipairs(files) do
_p(1, '%s', file)
end
_p(')')
return
end
local oldGetDefaultSeparator = path.getDefaultSeparator
path.getDefaultSeparator = function() return "/" end
if prj.kind == 'StaticLib' then
_p('add_library("%s" STATIC', prj.name)
elseif prj.kind == 'SharedLib' then
_p('add_library("%s" SHARED', prj.name)
elseif prj.kind == 'Makefile' then
-- Makefile projects become custom targets
_p('add_custom_target("%s"', prj.name)
for _, file in ipairs(generator_expression(prj, m.files, table_expression)) do
_p(1, '%s', file)
end
_p(')')
-- Makefile projects use buildcommands/rebuildcommands/cleancommands
-- which are handled through pre/post build
generate_prebuild(prj)
generate_postbuild(prj)
path.getDefaultSeparator = oldGetDefaultSeparator
return
else
if prj.executable_suffix then
_p('set(CMAKE_EXECUTABLE_SUFFIX "%s")', prj.executable_suffix)
end
-- WindowedApp gets the WIN32 flag for Windows subsystem
if prj.kind == 'WindowedApp' then
_p('add_executable("%s" WIN32', prj.name)
else
_p('add_executable("%s"', prj.name)
end
end
for _, file in ipairs(generator_expression(prj, m.files, table_expression)) do
_p(1, '%s', file);
end
_p(')')
-- output name
_p(0, 'set_target_properties("%s" PROPERTIES OUTPUT_NAME %s)', prj.name, generator_expression(prj, function(cfg) return {cfg.buildtarget.basename} end, one_expression))
-- target prefix/suffix/extension overrides
local targetprefix = generator_expression(prj, function(cfg)
if cfg.targetprefix and cfg.targetprefix ~= "" then
return {cfg.targetprefix}
end
return {}
end, one_expression)
if #targetprefix > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES PREFIX "%s")', prj.name, targetprefix)
end
--[[ Incompatible with premake-qt
local targetsuffix = generator_expression(prj, function(cfg)
if cfg.targetsuffix and cfg.targetsuffix ~= "" then
return {cfg.targetsuffix}
end
return {}
end, one_expression)
if #targetsuffix > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES SUFFIX "%s")', prj.name, targetsuffix)
end
local targetextension = generator_expression(prj, function(cfg)
if cfg.targetextension and cfg.targetextension ~= "" then
return {cfg.targetextension}
end
return {}
end, one_expression)
if #targetextension > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES SUFFIX "%s")', prj.name, targetextension)
end
--]]
-- output dir
_p(0, 'set_target_properties("%s" PROPERTIES', prj.name)
for cfg in project.eachconfig(prj) do
-- Multi-configuration generators appends a per-configuration subdirectory
-- to the specified directory (unless a generator expression is used)
-- for XXX_OUTPUT_DIRECTORY but not for XXX_OUTPUT_DIRECTORY_<CONFIG>
_p(1, 'ARCHIVE_OUTPUT_DIRECTORY_%s "%s"', cmake.cfgname(cfg):upper(), path.getrelative(prj.workspace.location, cfg.buildtarget.directory))
_p(1, 'LIBRARY_OUTPUT_DIRECTORY_%s "%s"', cmake.cfgname(cfg):upper(), path.getrelative(prj.workspace.location, cfg.buildtarget.directory))
_p(1, 'RUNTIME_OUTPUT_DIRECTORY_%s "%s"', cmake.cfgname(cfg):upper(), path.getrelative(prj.workspace.location, cfg.buildtarget.directory))
end
_p(0, ')')
-- object directory (intermediate directory)
local objdir = generator_expression(prj, function(cfg)
if cfg.objdir then
return {path.getrelative(prj.workspace.location, cfg.objdir)}
end
return {}
end, one_expression)
if #objdir > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES', prj.name)
for cfg in project.eachconfig(prj) do
if cfg.objdir then
_p(1, 'VS_INTERMEDIATE_DIRECTORY_%s "%s"', cmake.cfgname(cfg):upper(), path.getrelative(prj.workspace.location, cfg.objdir))
end
end
_p(0, ')')
end
-- dependencies (from links)
local dependencies = project.getdependencies(prj)
if #dependencies > 0 then
_p(0, 'add_dependencies("%s"', prj.name)
for _, dependency in ipairs(dependencies) do
_p(1, '"%s"', dependency.name)
end
_p(0,')')
end
-- dependson (non-linking build order dependencies)
local dependson_list = generator_expression(prj, function(cfg)
if cfg.dependson and #cfg.dependson > 0 then
return cfg.dependson
end
return {}
end, table_expression)
if #dependson_list > 0 then
_p(0, 'add_dependencies("%s"', prj.name)
for _, dep in ipairs(dependson_list) do
_p(1, '"%s"', dep)
end
_p(0, ')')
end
-- include dirs
local externalincludedirs = generator_expression(prj, function(cfg) return cfg.externalincludedirs end, table_expression)
if #externalincludedirs > 0 then
_p(0, 'target_include_directories("%s" SYSTEM PRIVATE', prj.name)
for _, dir in ipairs(externalincludedirs) do
_p(1, '%s', dir)
end
_p(0, ')')
end
local includedirs = generator_expression(prj, function(cfg) return cfg.includedirs end, table_expression)
if #includedirs > 0 then
_p(0, 'target_include_directories("%s" PRIVATE', prj.name)
for _, dir in ipairs(includedirs) do
_p(1, '%s', dir)
end
_p(0, ')')
end
local msvc_frameworkdirs = generator_expression(prj, function(cfg) return p.tools.msc.getincludedirs(cfg, {}, {}, cfg.frameworkdirs, cfg.includedirsafter) end)
local gcc_frameworkdirs = generator_expression(prj, function(cfg) return p.tools.gcc.getincludedirs(cfg, {}, {}, cfg.frameworkdirs, cfg.includedirsafter) end)
if #msvc_frameworkdirs > 0 or #gcc_frameworkdirs > 0 then
_p(0, 'if (MSVC)')
_p(1, 'target_compile_options("%s" PRIVATE %s)', prj.name, msvc_frameworkdirs)
_p(0, 'else()')
_p(1, 'target_compile_options("%s" PRIVATE %s)', prj.name, gcc_frameworkdirs)
_p(0, 'endif()')
end
local msvc_forceincludes = generator_expression(prj, function(cfg) return p.tools.msc.getforceincludes(cfg) end)
local gcc_forceincludes = generator_expression(prj, function(cfg) return p.tools.gcc.getforceincludes(cfg) end)
if #msvc_forceincludes > 0 or #gcc_forceincludes > 0 then
_p(0, '# force include')
_p(0, 'if (MSVC)')
_p(1, 'target_compile_options("%s" PRIVATE %s)', prj.name, msvc_forceincludes)
_p(0, 'else()')
_p(1, 'target_compile_options("%s" PRIVATE %s)', prj.name, gcc_forceincludes)
_p(0, 'endif()')
end
-- defines
local defines = generator_expression(prj, function(cfg) return m.esc(cfg.defines) end, table_expression) --p.esc(define):gsub(' ', '\\ ')
if #defines > 0 then
_p(0, 'target_compile_definitions("%s" PRIVATE', prj.name)
for _, define in ipairs(defines) do
_p(1, '%s', define)
end
_p(0, ')')
end
local msvc_undefines = generator_expression(prj, function(cfg) return p.tools.msc.getundefines(cfg.undefines) end)
local gcc_undefines = generator_expression(prj, function(cfg) return p.tools.gcc.getundefines(cfg.undefines) end)
if #msvc_undefines > 0 or #gcc_undefines > 0 then
_p(0, 'if (MSVC)')
_p(1, 'target_compile_options("%s" PRIVATE %s)', prj.name, msvc_undefines)
_p(0, 'else()')
_p(1, 'target_compile_options("%s" PRIVATE %s)', prj.name, gcc_undefines)
_p(0, 'endif()')
end
-- character set defines
local charset_defines = generator_expression(prj, function(cfg)
local res = {}
if cfg.characterset == "Unicode" then
table.insert(res, "UNICODE")
table.insert(res, "_UNICODE")
elseif cfg.characterset == "MBCS" then
table.insert(res, "_MBCS")
end
return res
end, table_expression)
if #charset_defines > 0 then
_p(0, 'target_compile_definitions("%s" PRIVATE', prj.name)
for _, define in ipairs(charset_defines) do
_p(1, '%s', define)
end
_p(0, ')')
end
-- setting build options
local all_build_options = generator_expression(prj, function(cfg) return m.unquote(cfg.buildoptions) end, table_expression)
if #all_build_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, option in ipairs(all_build_options) do
_p(1, '%s', option)
end
_p(0, ')')
end
-- C++ standard
local cppdialect = generator_expression(prj, function(cfg)
if (cfg.cppdialect ~= nil and cfg.cppdialect ~= '') or cfg.cppdialect == 'Default' then
local standard = {
["C++98"] = 98,
["C++0x"] = 11,
["C++11"] = 11,
["C++1y"] = 14,
["C++14"] = 14,
["C++1z"] = 17,
["C++17"] = 17,
["C++2a"] = 20,
["C++20"] = 20,
["C++2b"] = 23,
["C++23"] = 23,
["C++latest"] = 23,
["gnu++98"] = 98,
["gnu++0x"] = 11,
["gnu++11"] = 11,
["gnu++1y"] = 14,
["gnu++14"] = 14,
["gnu++1z"] = 17,
["gnu++17"] = 17,
["gnu++2a"] = 20,
["gnu++20"] = 20,
["gnu++2b"] = 23,
["gnu++23"] = 23,
}
local val = standard[cfg.cppdialect]
if val then
return { tostring(val) }
end
end
return {}
end, one_expression)
if #cppdialect > 0 then
local extension = generator_expression(prj, function(cfg) return iif(cfg.cppdialect:find('^gnu') == nil, {'NO'}, {'YES'}) end, one_expression)
_p(0, 'set_target_properties("%s" PROPERTIES', prj.name)
_p(1, 'CXX_STANDARD %s', cppdialect)
_p(1, 'CXX_STANDARD_REQUIRED YES')
_p(1, 'CXX_EXTENSIONS %s', extension)
_p(0, ')')
end
-- C standard
local cdialect = generator_expression(prj, function(cfg)
if cfg.cdialect ~= nil and cfg.cdialect ~= '' and cfg.cdialect ~= 'Default' then
local standard = {
["C89"] = 90,
["C90"] = 90,
["C99"] = 99,
["C11"] = 11,
["C17"] = 17,
["C23"] = 23,
["gnu89"] = 90,
["gnu90"] = 90,
["gnu99"] = 99,
["gnu11"] = 11,
["gnu17"] = 17,
["gnu23"] = 23,
}
local val = standard[cfg.cdialect]
if val then
return { tostring(val) }
end
end
return {}
end, one_expression)
if #cdialect > 0 then
local c_extension = generator_expression(prj, function(cfg) return iif(cfg.cdialect and cfg.cdialect:find('^gnu') ~= nil, {'YES'}, {'NO'}) end, one_expression)
_p(0, 'set_target_properties("%s" PROPERTIES', prj.name)
_p(1, 'C_STANDARD %s', cdialect)
_p(1, 'C_STANDARD_REQUIRED YES')
_p(1, 'C_EXTENSIONS %s', c_extension)
_p(0, ')')
end
-- Position Independent Code (independent of dialect)
local pic = generator_expression(prj, function(cfg)
if cfg.pic == 'On' then
return {'True'}
elseif cfg.pic == 'Off' then
return {'False'}
end
return {}
end, one_expression)
if #pic > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES POSITION_INDEPENDENT_CODE %s)', prj.name, pic)
end
-- Link-Time Optimization (independent of dialect)
local lto = generator_expression(prj, function(cfg)
if cfg.linktimeoptimization and cfg.linktimeoptimization ~= "Off" and cfg.linktimeoptimization ~= "Default" then
return {'True'}
elseif cfg.linktimeoptimization == "Off" then
return {'False'}
end
return {}
end, one_expression)
if #lto > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES INTERPROCEDURAL_OPTIMIZATION %s)', prj.name, lto)
end
-- compileas: force compilation language for source files
local compileas = generator_expression(prj, function(cfg)
if cfg.compileas == "C" then
return {"C"}
elseif cfg.compileas == "C++" then
return {"CXX"}
end
return {}
end, one_expression)
if #compileas > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES LINKER_LANGUAGE %s)', prj.name, compileas)
end
-- MSVC runtime library
local msvc_runtime = generator_expression(prj, function(cfg)
local rt = ""
if cfg.staticruntime == "On" then
rt = "MultiThreaded"
elseif cfg.staticruntime == "Off" or cfg.staticruntime == "Default" or cfg.staticruntime == nil then
rt = "MultiThreadedDLL"
else
return {}
end
if cfg.runtime == "Debug" then
rt = rt .. "Debug"
end
return {rt}
end, one_expression)
if #msvc_runtime > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES MSVC_RUNTIME_LIBRARY "%s")', prj.name, msvc_runtime)
end
-- Optimization
local optimize_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.optimize then
if cfg.optimize == "Off" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Od>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-O0>')
elseif cfg.optimize == "On" or cfg.optimize == "Debug" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Og>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Og>')
elseif cfg.optimize == "Size" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/O1>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Os>')
elseif cfg.optimize == "Speed" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/O2>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-O2>')
elseif cfg.optimize == "Full" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Ox>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-O3>')
end
end
return res
end, table_expression)
if #optimize_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(optimize_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Debug symbols
local symbols_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.symbols then
if cfg.symbols == "On" or cfg.symbols == "Full" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Zi>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-g>')
elseif cfg.symbols == "FastLink" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/ZI>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-g>')
elseif cfg.symbols == "Off" then
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-g0>')
end
end
return res
end, table_expression)
if #symbols_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(symbols_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Debug format (SplitDwarf etc.)
local debugformat_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.debugformat then
if cfg.debugformat == "SplitDwarf" then
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-gsplit-dwarf>')
elseif cfg.debugformat == "Dwarf" then
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-gdwarf>')
elseif cfg.debugformat == "c7" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Z7>')
end
end
return res
end, table_expression)
if #debugformat_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(debugformat_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Warnings
local warnings_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.warnings then
if cfg.warnings == "Off" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/W0>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-w>')
elseif cfg.warnings == "Default" then
-- default, no flags needed
elseif cfg.warnings == "High" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/W4>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall>')
elseif cfg.warnings == "Extra" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/W4>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wextra>')
elseif cfg.warnings == "Everything" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Wall>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Weverything>')
end
end
return res
end, table_expression)
if #warnings_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(warnings_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- External warnings (for system/external includes)
local externalwarnings_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.externalwarnings then
if cfg.externalwarnings == "Off" then
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-system-headers>')
elseif cfg.externalwarnings == "Default" then
-- default, no flags needed
elseif cfg.externalwarnings == "High" or cfg.externalwarnings == "Extra" or cfg.externalwarnings == "Everything" then
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wsystem-headers>')
end
end
return res
end, table_expression)
if #externalwarnings_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(externalwarnings_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Enable/disable specific warnings
local enablewarnings_opts = generator_expression(prj, function(cfg)
local res = {}
if cfg.enablewarnings and #cfg.enablewarnings > 0 then
for _, w in ipairs(cfg.enablewarnings) do
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/w1' .. w .. '>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-W' .. w .. '>')
end
end
return res
end, table_expression)
if #enablewarnings_opts > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(enablewarnings_opts) do
_p(1, '%s', opt)
end
_p(0, ')')
end
local disablewarnings_opts = generator_expression(prj, function(cfg)
local res = {}
if cfg.disablewarnings and #cfg.disablewarnings > 0 then
for _, w in ipairs(cfg.disablewarnings) do
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/wd' .. w .. '>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-' .. w .. '>')
end
end
return res
end, table_expression)
if #disablewarnings_opts > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(disablewarnings_opts) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Fatal warnings (treat warnings as errors)
local fatalwarnings_opts = generator_expression(prj, function(cfg)
local res = {}
if cfg.fatalwarnings and #cfg.fatalwarnings > 0 then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/WX>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Werror>')
end
return res
end, table_expression)
if #fatalwarnings_opts > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(fatalwarnings_opts) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- RTTI
local rtti_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.rtti then
if cfg.rtti == "Off" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/GR->')
table.insert(res, '$<$<AND:$<NOT:$<CXX_COMPILER_ID:MSVC>>,$<COMPILE_LANGUAGE:CXX>>:-fno-rtti>')
elseif cfg.rtti == "On" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/GR>')
table.insert(res, '$<$<AND:$<NOT:$<CXX_COMPILER_ID:MSVC>>,$<COMPILE_LANGUAGE:CXX>>:-frtti>')
end
end
return res
end, table_expression)
if #rtti_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(rtti_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Exception handling
local exceptions_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.exceptionhandling then
if cfg.exceptionhandling == "Off" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/EHs-c->')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fno-exceptions>')
elseif cfg.exceptionhandling == "On" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/EHsc>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>')
elseif cfg.exceptionhandling == "SEH" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/EHa>')
elseif cfg.exceptionhandling == "CThrow" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/EHsc>')
end
end
return res
end, table_expression)
if #exceptions_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(exceptions_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Floating point model
local floatingpoint_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.floatingpoint then
if cfg.floatingpoint == "Fast" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/fp:fast>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-ffast-math>')
elseif cfg.floatingpoint == "Strict" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/fp:strict>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-ffloat-store>')
end
end
return res
end, table_expression)
if #floatingpoint_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(floatingpoint_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Symbol visibility
local visibility_val = generator_expression(prj, function(cfg)
if cfg.visibility then
local vis_map = {
["Default"] = "default",
["Hidden"] = "hidden",
["Internal"] = "internal",
["Protected"] = "protected",
}
local v = vis_map[cfg.visibility]
if v then
return {v}
end
end
return {}
end, one_expression)
if #visibility_val > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES', prj.name)
_p(1, 'C_VISIBILITY_PRESET %s', visibility_val)
_p(1, 'CXX_VISIBILITY_PRESET %s', visibility_val)
_p(0, ')')
end
-- Inline visibility
local inlinesvisibility_val = generator_expression(prj, function(cfg)
if cfg.inlinesvisibility == "Hidden" then
return {"YES"}
end
return {}
end, one_expression)
if #inlinesvisibility_val > 0 then
_p(0, 'set_target_properties("%s" PROPERTIES VISIBILITY_INLINES_HIDDEN %s)', prj.name, inlinesvisibility_val)
end
-- Omit frame pointer
local omitframepointer_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.omitframepointer then
if cfg.omitframepointer == "On" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Oy>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fomit-frame-pointer>')
elseif cfg.omitframepointer == "Off" then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/Oy->')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fno-omit-frame-pointer>')
end
end
return res
end, table_expression)
if #omitframepointer_options > 0 then
_p(0, 'target_compile_options("%s" PRIVATE', prj.name)
for _, opt in ipairs(omitframepointer_options) do
_p(1, '%s', opt)
end
_p(0, ')')
end
-- Unsigned char
local unsignedchar_options = generator_expression(prj, function(cfg)
local res = {}
if cfg.unsignedchar then
table.insert(res, '$<$<CXX_COMPILER_ID:MSVC>:/J>')
table.insert(res, '$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-funsigned-char>')