-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcmake.cpp
More file actions
3708 lines (3234 loc) · 129 KB
/
cmake.cpp
File metadata and controls
3708 lines (3234 loc) · 129 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
/*
* Copyright (C) 2016-2017, Egor Pugin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cmake.h"
#include <access_table.h>
#include <database.h>
#include <directories.h>
#include <exceptions.h>
#include <hash.h>
#include <lock.h>
#include <inserts.h>
#include <program.h>
#include <resolver.h>
#include <settings.h>
#include <boost/algorithm/string.hpp>
#include <primitives/command.h>
#include <primitives/date_time.h>
#include <primitives/executor.h>
#ifdef _WIN32
#include <primitives/win32helpers.h>
#endif
#ifdef _WIN32
#include <WinReg.hpp>
#endif
#include <primitives/log.h>
//DECLARE_STATIC_LOGGER(logger, "cmake");
String repeat(const String &e, int n);
// common?
const String cppan_project_name = "__cppan";
const String exports_dir_name = "exports";
const String exports_dir = "${CMAKE_BINARY_DIR}/" + exports_dir_name + "/";
const String cppan_ide_folder = "CPPAN Targets";
const String packages_folder = cppan_ide_folder + "/Packages";
const String service_folder = cppan_ide_folder + "/Service";
const String service_deps_folder = service_folder + "/Dependencies";
const String dependencies_folder = cppan_ide_folder + "/Dependencies";
const String local_dependencies_folder = dependencies_folder + "/Local";
//
const String cmake_config_filename = "CMakeLists.txt";
const String cppan_build_dir = "build";
const String cmake_functions_filename = "functions.cmake";
const String cmake_header_filename = "header.cmake";
const String cppan_cmake_config_filename = "CPPANConfig.cmake";
const String cmake_export_import_filename = "export.cmake";
const String cmake_helpers_filename = "helpers.cmake";
const String cppan_stamp_filename = "cppan_sources.stamp";
const String cppan_checks_yml = "checks.yml";
const String parallel_checks_file = "vars.txt";
const String cmake_src_actions_filename = "actions.cmake";
const String cmake_src_include_guard_filename = "include.cmake";
const String cmake_cppan_location_filename = "cppan_location.cmake";
const String cmake_obj_build_filename = "build.cmake";
const String cmake_obj_generate_filename = "generate.cmake";
const String cmake_obj_exports_filename = "exports.cmake";
const String cmake_obj_include_script_filename = "include_script.cmake";
const String cmake_minimum_required = "cmake_minimum_required(VERSION 3.2.0)";
const String cmake_debug_message_fun = "cppan_debug_message";
const String cppan_dummy_build_target = "b"; // build
const String cppan_dummy_copy_target = "c"; // copy
const String debug_stack_space_diff = repeat(" ", 4);
const String config_delimeter_short = repeat("#", 40);
const String config_delimeter = repeat(config_delimeter_short, 2);
const String cmake_includes = R"(
include(CheckCXXSymbolExists)
include(CheckFunctionExists)
include(CheckIncludeFiles)
include(CheckIncludeFile)
include(CheckIncludeFileCXX)
include(CheckLibraryExists)
include(CheckTypeSize)
include(CheckCSourceCompiles)
include(CheckCSourceRuns)
include(CheckCXXSourceCompiles)
include(CheckCXXSourceRuns)
include(CheckStructHasMember)
include(GenerateExportHeader)
include(TestBigEndian)
)";
class ScopedDependencyCondition
{
CMakeEmitter &ctx;
const Package &d;
bool empty_lines;
public:
ScopedDependencyCondition(CMakeEmitter &ctx, const Package &d, bool empty_lines = true)
: ctx(ctx), d(d), empty_lines(empty_lines)
{
if (d.conditions.empty())
return;
ctx.addLine("# conditions for dependency: " + d.target_name);
for (auto &c : d.conditions)
if (!c.empty())
ctx.if_(c);
}
~ScopedDependencyCondition()
{
if (d.conditions.empty())
return;
for (auto &c[[maybe_unused]] : d.conditions)
if (!c.empty())
ctx.endif();
if (empty_lines)
ctx.emptyLines();
}
};
void registerCmakePackage()
{
#ifdef _WIN32
// if we write into HKLM, we won't be able to access the pkg file in admins folder
winreg::RegKey icon(/*is_elevated() ? HKEY_LOCAL_MACHINE : */HKEY_CURRENT_USER, L"Software\\Kitware\\CMake\\Packages\\CPPAN");
icon.SetStringValue(L"", directories.get_static_files_dir().wstring().c_str());
write_file_if_different(directories.get_static_files_dir() / cppan_cmake_config_filename, cppan_cmake_config);
#else
auto cppan_cmake_dir = get_home_directory() / ".cmake" / "packages";
write_file_if_different(cppan_cmake_dir / "CPPAN" / "1", cppan_cmake_dir.string());
write_file_if_different(cppan_cmake_dir / cppan_cmake_config_filename, cppan_cmake_config);
#endif
}
String cmake_debug_message(const String &s)
{
if (!Settings::get_local_settings().debug_generated_cmake_configs)
return "";
return cmake_debug_message_fun + "(\"" + s + "\")";
}
String repeat(const String &e, int n)
{
String s;
if (n < 0)
return s;
s.reserve(e.size() * n);
for (int i = 0; i < n; i++)
s += e;
return s;
}
auto normalize_path2(const path &p)
{
return to_printable_string(normalize_path(p));
}
void config_section_title(CMakeEmitter &ctx, const String &t, bool nodebug = false)
{
ctx.emptyLines();
ctx.addLine(config_delimeter);
ctx.addLine("#");
ctx.addLine("# " + t);
ctx.addLine("#");
ctx.addLine(config_delimeter);
ctx.addLine();
if (!nodebug)
ctx.addLine(cmake_debug_message("Section: " + t));
ctx.emptyLines();
}
void file_header(CMakeEmitter &ctx, const Package &d, bool root)
{
if (!d.empty())
{
ctx.addLine("#");
ctx.addLine("# cppan");
ctx.addLine("# package: " + d.ppath.toString());
ctx.addLine("# version: " + d.version.toString());
ctx.addLine("#");
ctx.addLine("# source dir: " + normalize_path2(d.getDirSrc().string()));
ctx.addLine("# binary dir: " + normalize_path2(d.getDirObj().string()));
ctx.addLine("#");
ctx.addLine("# package hash : " + d.getHash());
ctx.addLine("# package hash short: " + d.getHashShort());
ctx.addLine("#");
}
else
{
ctx.addLine("#");
ctx.addLine("# cppan");
ctx.addLine("#");
}
config_section_title(ctx, "header", true);
ctx.addLine("include(" + normalize_path2(directories.get_static_files_dir() / cmake_header_filename) + ")");
ctx.addLine();
if (!Settings::get_local_settings().debug_generated_cmake_configs)
return;
// before header message
if (!root)
{
ctx.addLine("set(CPPAN_DEBUG_STACK_SPACE \"${CPPAN_DEBUG_STACK_SPACE}" + debug_stack_space_diff + "\" CACHE STRING \"\" FORCE)");
ctx.addLine();
}
if (!d.empty())
{
ctx.addLine(cmake_debug_message("Entering file: ${CMAKE_CURRENT_LIST_FILE}"));
ctx.addLine(cmake_debug_message("Package : " + d.target_name));
}
else
ctx.addLine(cmake_debug_message("Entering file: ${CMAKE_CURRENT_LIST_FILE}"));
ctx.addLine();
ctx.addLine(config_delimeter);
ctx.addLine();
}
void file_footer(CMakeEmitter &ctx, const Package &d)
{
if (!Settings::get_local_settings().debug_generated_cmake_configs)
return;
config_section_title(ctx, "footer", true);
ctx.addLine(cmake_debug_message("Leaving file: ${CMAKE_CURRENT_LIST_FILE}"));
ctx.addLine();
// after footer message
ctx.addLine("string(LENGTH \"${CPPAN_DEBUG_STACK_SPACE}\" len)");
ctx.addLine("math(EXPR len \"${len}-" + std::to_string(debug_stack_space_diff.size()) + "\")");
ctx.if_("NOT ${len} LESS 0");
ctx.addLine("string(SUBSTRING \"${CPPAN_DEBUG_STACK_SPACE}\" 0 ${len} CPPAN_DEBUG_STACK_SPACE)");
ctx.else_();
ctx.addLine("set(CPPAN_DEBUG_STACK_SPACE \"\")");
ctx.endif();
ctx.addLine("set(CPPAN_DEBUG_STACK_SPACE \"${CPPAN_DEBUG_STACK_SPACE}\" CACHE STRING \"\" FORCE)");
ctx.addLine();
ctx.addLine(config_delimeter);
ctx.addLine();
//ctx.splitLines();
}
void print_storage_dirs(CMakeEmitter &ctx)
{
config_section_title(ctx, "storage dirs");
ctx.addLine("set_cache_var(STORAGE_DIR \"" + normalize_path2(directories.storage_dir) + "\")");
ctx.addLine("set_cache_var(STORAGE_DIR_ETC \"" + normalize_path2(directories.storage_dir_etc) + "\")");
ctx.addLine("set_cache_var(STORAGE_DIR_ETC_STATIC \"" + normalize_path2(directories.get_static_files_dir()) + "\")");
ctx.addLine("set_cache_var(STORAGE_DIR_USR \"" + normalize_path2(directories.storage_dir_usr) + "\")");
ctx.addLine();
}
void print_local_project_files(CMakeEmitter &ctx, const Project &p)
{
ctx.increaseIndent("set(src");
for (auto &f : FilesSorted(p.files.begin(), p.files.end()))
ctx.addLine("\"" + normalize_path2(f) + "\"");
ctx.decreaseIndent(")");
}
String add_target(const Package &p)
{
if (p.flags[pfExecutable])
return "add_executable";
return "add_library";
}
String add_target_suffix(const String &t)
{
auto &s = Settings::get_local_settings().meta_target_suffix;
if (!s.empty())
return t + "-" + s;
return t;
}
String cppan_dummy_target(const String &name)
{
String t = "cppan-d";
if (!name.empty())
t += "-" + name;
return add_target_suffix(t);
}
void set_target_properties(CMakeEmitter &ctx, const String &name, const String &property, const String &value)
{
ctx.addLine("set_target_properties(" + name + " PROPERTIES " + property + " " + value + ")");
}
void set_target_properties(CMakeEmitter &ctx, const String &property, const String &value)
{
set_target_properties(ctx, "${this}", property, value);
}
void declare_dummy_target(CMakeEmitter &ctx, const String &name)
{
config_section_title(ctx, "dummy compiled target " + name);
ctx.addLine("# this target will be always built before any other");
ctx.if_("VISUAL_STUDIO");
ctx.addLine("add_custom_target(" + cppan_dummy_target(name) + " ALL DEPENDS cppan_intentionally_missing_file.txt)");
ctx.elseif("NINJA");
ctx.addLine("add_custom_target(" + cppan_dummy_target(name) + " ALL)");
ctx.else_();
ctx.addLine("add_custom_target(" + cppan_dummy_target(name) + " ALL)");
ctx.endif();
ctx.addLine();
set_target_properties(ctx, cppan_dummy_target(name), "FOLDER", "\"" + service_folder + "\"");
ctx.emptyLines();
}
void print_solution_folder(CMakeEmitter &ctx, const String &target, const path &folder)
{
set_target_properties(ctx, target, "FOLDER", "\"" + normalize_path2(folder) + "\"");
}
String add_subdirectory(String src)
{
normalize_string(src);
return "cppan_include(\"" + src + "/" + cmake_src_include_guard_filename + "\")";
}
void add_subdirectory(CMakeEmitter &ctx, const String &src)
{
ctx.addLine(add_subdirectory(src));
}
String prepare_include_directory(const String &i)
{
if (i.find("${") == 0)
return i;
return "${SDIR}/" + i;
};
void print_sdir_bdir(CMakeEmitter &ctx, const Package &d)
{
if (d.flags[pfLocalProject])
ctx.addLine("set(SDIR " + normalize_path2(rd[d].config->getDefaultProject().root_directory) + ")");
else
ctx.addLine("set(SDIR ${CMAKE_CURRENT_SOURCE_DIR})");
ctx.addLine("set(BDIR ${CMAKE_CURRENT_BINARY_DIR})");
ctx.addLine("set(BDIR_PRIVATE ${BDIR}/cppan_private)");
ctx.addLine("execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${BDIR_PRIVATE})");
ctx.emptyLines();
}
String get_binary_path(const Package &d, const String &prefix)
{
return prefix + "/cppan/" + d.getHashShort();
}
String get_binary_path(const Package &d)
{
return get_binary_path(d, "${CMAKE_BINARY_DIR}");
}
void gather_all_deps(const Packages &dd, Packages &out)
{
for (auto &dp : dd)
{
auto &d = dp.second;
auto i = out.insert(dp);
if (i.second)
gather_all_deps(rd[d].dependencies, out);
}
}
void print_dependencies(CMakeEmitter &ctx, const Package &d, bool use_cache)
{
const auto &dd = rd[d].dependencies;
if (dd.empty())
return;
std::vector<Package> includes;
CMakeEmitter ctx2, ctx_actions;
config_section_title(ctx, "direct dependencies");
// make sure this var is 0
//ctx.addLine("set(CPPAN_BUILD_EXECUTABLES_WITH_SAME_CONFIG 0)");
//ctx.addLine();
for (auto &p : dd)
{
auto &dep = p.second;
ScopedDependencyCondition sdc(ctx, dep);
if (dep.flags[pfLocalProject])
ctx.addLine("set_cache_var(" + dep.variable_no_version_name + "_DIR " + normalize_path2(rd[dep].config->getDefaultProject().root_directory) + ")");
else
ctx.addLine("set_cache_var(" + dep.variable_no_version_name + "_DIR " + normalize_path2(dep.getDirSrc()) + ")");
}
ctx.emptyLines();
for (auto &p : dd)
{
auto &dep = p.second;
const auto dir = [&dep, &use_cache]
{
// do not "optimize" this condition (whole if..else)
if (dep.flags[pfHeaderOnly] || dep.flags[pfIncludeDirectoriesOnly])
return dep.getDirSrc();
else if (use_cache)
return dep.getDirObj();
else
return dep.getDirSrc();
}();
if (dep.flags[pfIncludeDirectoriesOnly])
{
// MUST be here!
// actions are executed from include_directories only projects
ScopedDependencyCondition sdc(ctx_actions, dep);
ctx_actions.addLine("# " + dep.target_name);
ctx_actions.addLine("cppan_include(\"" + normalize_path2(dir / cmake_src_actions_filename) + "\")");
}
else if (!use_cache || dep.flags[pfHeaderOnly])
{
ScopedDependencyCondition sdc(ctx, dep);
ctx.addLine("# " + dep.target_name);
add_subdirectory(ctx, dir.string());
}
else if (dep.flags[pfLocalProject])
{
ScopedDependencyCondition sdc(ctx, dep);
ctx.if_("NOT TARGET " + dep.target_name + "");
ctx.if_("CPPAN_USE_CACHE");
ctx.addLine("add_subdirectory(\"" + normalize_path2(dir) + "\" \"" + normalize_path2(dep.getDirObj() / "build/${config_dir}") + "\")");
ctx.else_();
ctx.addLine("add_subdirectory(\"" + normalize_path2(dir) + "\" \"" + get_binary_path(dep) + "\")");
ctx.endif();
ctx.endif();
}
else
{
// add local build includes
ScopedDependencyCondition sdc2(ctx2, dep);
ctx2.addLine("# " + dep.target_name);
add_subdirectory(ctx2, dep.getDirSrc().string());
includes.push_back(dep);
}
}
ctx.addLine();
if (!includes.empty())
{
config_section_title(ctx, "include dependencies (they should be placed at the end)");
ctx.if_("CPPAN_USE_CACHE");
if (!d.empty())
{
ctx.addLine("set(CPPAN_BUILD_EXECUTABLES_WITH_SAME_CONFIG "s + (
rd[d].config->getDefaultProject().build_dependencies_with_same_config ? "1" : "0") + ")");
ctx.addLine();
}
for (auto &dep : includes)
{
ScopedDependencyCondition sdc(ctx, dep);
ctx.addLine("# " + dep.target_name + "\n" +
"cppan_include(\"" + normalize_path2(dep.getDirObj() / cmake_obj_generate_filename) + "\")");
}
ctx.else_();
ctx.addLine(boost::trim_copy(ctx2.getText()));
ctx.endif();
/*
cmake currently does not support aliases to imported targets
config_section_title(ctx, "aliases");
for (auto &dep : includes)
{
add_aliases(ctx, dep, [](const Package &d, const String &s)
{
return add_target(d) + "(" + s + " ALIAS " + d.target_name_hash + ")";
});
}*/
}
for (auto &p : dd)
{
auto &dep = p.second;
ScopedDependencyCondition sdc(ctx, dep);
if (dep.flags[pfLocalProject])
ctx.addLine("set_cache_var(" + dep.variable_no_version_name + "_DIR " + normalize_path2(rd[dep].config->getDefaultProject().root_directory) + ")");
else
ctx.addLine("set_cache_var(" + dep.variable_no_version_name + "_DIR " + normalize_path2(dep.getDirSrc()) + ")");
}
// after all deps
ctx += ctx_actions;
// include scripts
CMakeEmitter ctx_includes;
bool print_includes = false;
config_section_title(ctx_includes, "include scripts");
auto &all_deps = rd[d].include_script_deps;
if (!all_deps)
{
all_deps.emplace();
Packages out;
gather_all_deps(dd, out);
StringMap<Package> real_values;
for (auto &p : out)
{
auto &dep = p.second;
if (rd[dep].config->getDefaultProject().include_script.empty())
continue;
real_values.insert(p);
}
all_deps = real_values;
}
for (auto &p : all_deps.value())
{
auto &dep = p.second;
if (rd[dep].config->getDefaultProject().include_script.empty())
continue;
print_includes = true;
ScopedDependencyCondition sdc(ctx_includes, dep);
ctx_includes.addLine("# " + dep.target_name + "\n" +
"include(" + normalize_path2(dep.getDirObj()) + "/" + cmake_obj_include_script_filename + ")");
}
if (!rd[d].config->getDefaultProject().include_script.empty())
{
// print self script
ctx_includes.addLine("# " + d.target_name + "\n" +
"include(" + normalize_path2(d.getDirObj()) + "/" + cmake_obj_include_script_filename + ")");
}
if (print_includes)
ctx += ctx_includes;
//ctx.splitLines();
}
void gather_build_deps(const Packages &dd, Packages &out, bool recursive = false, int depth = 0)
{
for (auto &dp : dd)
{
auto &d = dp.second;
if (d.flags[pfHeaderOnly] || d.flags[pfIncludeDirectoriesOnly])
continue;
// add only direct dependencies of the invocation package
if (d.flags[pfExecutable])
{
if (depth == 0)
out.insert(dp);
continue;
}
auto i = out.insert(dp);
if (i.second && recursive)
gather_build_deps(rd[d].dependencies, out, recursive, depth + 1);
}
}
void gather_copy_deps(const Packages &dd, Packages &out)
{
for (auto &dp : dd)
{
auto &d = dp.second;
if (d.flags[pfHeaderOnly] || d.flags[pfIncludeDirectoriesOnly])
continue;
if (d.flags[pfExecutable])
{
if (!rd[d].config->getDefaultProject().copy_to_output_dir)
continue;
if (!Settings::get_local_settings().copy_all_libraries_to_output)
{
if (!d.flags[pfLocalProject])
continue;
if (d.flags[pfLocalProject] && !d.flags[pfDirectDependency])
continue; // but maybe copy its deps?
}
else
{
// copy only direct executables
if (!d.flags[pfDirectDependency])
continue;
}
}
auto i = out.insert(dp);
if (i.second)
gather_copy_deps(rd[d].dependencies, out);
}
}
auto run_command(const Settings &bs, primitives::Command &c)
{
if (bs.build_system_verbose)
c.inherit = true;
std::error_code ec;
c.execute(ec);
if (ec)
throw std::runtime_error("Run command '" + c.print() + "', error: " + boost::trim_copy(ec.message()));
if (!bs.build_system_verbose)
LOG_INFO(logger, "Ok");
return c.exit_code;
}
auto library_api(const Package &d)
{
// we use short hash to avoid cl.exe issues with long command line
return CPPAN_EXPORT_PREFIX + d.target_name_hash;
//return CPPAN_EXPORT_PREFIX + d.variable_name;
}
void load_cppan_command_unconditionally(CMakeEmitter &ctx)
{
ctx.addLine("include(\"" + normalize_path2(directories.get_static_files_dir() / cmake_cppan_location_filename) + "\")");
}
void load_cppan_command(CMakeEmitter &ctx)
{
ctx.if_("NOT CPPAN_COMMAND");
load_cppan_command_unconditionally(ctx);
ctx.endif();
ctx.addLine();
}
void CMakePrinter::print_build_dependencies(CMakeEmitter &ctx, const String &target) const
{
// direct deps' build actions for non local build
config_section_title(ctx, "build dependencies");
// build deps
ctx.if_("CPPAN_USE_CACHE");
// Run building of dependencies before project building.
// We build all deps because if some dep is removed,
// build system give you and error about this.
Packages build_deps;
gather_build_deps(rd[d].dependencies, build_deps, true);
if (!build_deps.empty())
{
CMakeEmitter local;
local.addLine("set(CPPAN_GET_CHILDREN_VARIABLES 1)");
local.addLine("get_configuration_with_generator(config)"); // children
local.if_("CPPAN_BUILD_EXECUTABLES_WITH_SAME_CONFIG");
local.addLine("get_configuration_with_generator(config_exe)");
local.else_();
local.addLine("get_configuration_exe(config_exe)");
local.endif();
local.addLine("set(CPPAN_GET_CHILDREN_VARIABLES 0)");
local.emptyLines();
local.addLine("string(TOUPPER \"${CMAKE_BUILD_TYPE}\" CMAKE_BUILD_TYPE_UPPER)");
local.emptyLines();
// we're in helper, set this var to build target
if (d.empty())
local.addLine("set(this " + target + ")");
local.emptyLines();
// TODO: check with ninja and remove if ok
//Packages build_deps_all;
//gather_build_deps(rd[d].dependencies, build_deps_all, true);
//for (auto &dp : build_deps_all)
for (auto &dp : build_deps)
{
auto &p = dp.second;
// local projects are always built inside solution
if (p.flags[pfLocalProject])
continue;
ScopedDependencyCondition sdc(local, p);
local.addLine("get_target_property(implib_" + p.variable_name + " " + p.target_name + " IMPORTED_IMPLIB_${CMAKE_BUILD_TYPE_UPPER})");
local.addLine("get_target_property(imploc_" + p.variable_name + " " + p.target_name + " IMPORTED_LOCATION_${CMAKE_BUILD_TYPE_UPPER})");
local.addLine("get_target_property(impson_" + p.variable_name + " " + p.target_name + " IMPORTED_SONAME_${CMAKE_BUILD_TYPE_UPPER})");
}
local.emptyLines();
#define ADD_VAR(v) rest += "-D" #v "=${" #v "} "
String rest;
// we do not pass this var to children
//ADD_VAR(CPPAN_BUILD_EXECUTABLES_WITH_SAME_CONFIG);
ADD_VAR(CPPAN_BUILD_EXECUTABLES_WITH_SAME_CONFIGURATION);
ADD_VAR(CMAKE_BUILD_TYPE);
ADD_VAR(CPPAN_BUILD_VERBOSE);
ADD_VAR(CPPAN_BUILD_WARNING_LEVEL);
ADD_VAR(CPPAN_RC_ENABLED);
ADD_VAR(CPPAN_COPY_ALL_LIBRARIES_TO_OUTPUT);
ADD_VAR(N_CORES);
ADD_VAR(XCODE);
ADD_VAR(NINJA);
ADD_VAR(NINJA_FOUND);
ADD_VAR(VISUAL_STUDIO);
ADD_VAR(CLANG);
#undef ADD_VAR
local.addLine("set(rest \"" + rest + "\")");
local.emptyLines();
local.addLine(R"(set(ext sh)
if (WIN32)
set(ext bat)
endif()
)");
local.emptyLines();
if (d.empty())
local.addLine("set(file ${BDIR}/cppan_build_deps_$<CONFIG>.${ext})");
else
// FIXME: this is probably incorrect
local.addLine("set(file ${BDIR}/cppan_build_deps_" + d.target_name_hash + "_$<CONFIG>.${ext})");
local.emptyLines();
local.addLine(R"(#if (NOT CPPAN_BUILD_LEVEL)
#set(CPPAN_BUILD_LEVEL 0)
#else()
#math(EXPR CPPAN_BUILD_LEVEL "${CPPAN_BUILD_LEVEL} + 1")
#endif()
set(bat_file_error)
if (WIN32)
set(bat_file_error "@if %errorlevel% neq 0 goto :cmEnd")
endif()
set(at_symbol)
if (WIN32)
set(at_symbol @)
endif()
)");
bool has_build_deps = false;
for (auto &dp : build_deps)
{
auto &p = dp.second;
// local projects are always built inside solution
if (p.flags[pfLocalProject])
continue;
String cfg = "config";
if (p.flags[pfExecutable] && !p.flags[pfLocalProject])
cfg = "config_exe";
has_build_deps = true;
ScopedDependencyCondition sdc(local, p, false);
local.addLine("set(bd_" + p.variable_name + " \"");
//local.addLine("@echo Building " + p.target_name + ": ${" + cfg + "}");
local.addLine("${at_symbol}");
local.addText("\\\"${CMAKE_COMMAND}\\\" ");
//local.addText("-DCPPAN_BUILD_LEVEL=${CPPAN_BUILD_LEVEL} ");
//local.addText("-DTARGET_VAR=" + p.variable_name + " "); // remove!
//local.addText("-DTARGET_FILE=$<TARGET_FILE:" + p.target_name + "> ");
// this also probably must consider CPPAN_BUILD_EXECUTABLES_WITH_SAME_CONFIG
if (p.flags[pfExecutable] /* and not CPPAN_BUILD_EXECUTABLES_WITH_SAME_CONFIG */)
local.addText("-DCONFIG=Release ");
else
local.addText("-DCONFIG=$<CONFIG> ");
local.addText("-DBUILD_DIR=" + normalize_path2(p.getDirObj()) + "/build/${" + cfg + "} ");
local.addText("-DEXECUTABLE="s + (p.flags[pfExecutable] ? "1" : "0") + " ");
if (d.empty())
local.addText("-DMULTICORE=1 ");
local.addText("${rest} ");
local.addText("-P " + normalize_path2(p.getDirObj()) + "/" + cmake_obj_build_filename);
#ifndef _WIN32
// causes system overloads
//local.addText(" &");
#endif
local.addText("\n${bat_file_error}\")");
}
local.emptyLines();
local.addLine("set(bat_file_begin)");
local.if_("WIN32");
local.addLine("set(bat_file_begin @setlocal)");
local.addLine(R"(set(bat_file_error "\n
@exit /b 0
:cmEnd
@endlocal & @call :cmErrorLevel %errorlevel%
:cmErrorLevel
@exit /b %1
"))");
local.else_();
local.addLine("set(bat_file_begin \"#!/bin/sh\\n\")");
local.endif();
local.increaseIndent("file(GENERATE OUTPUT ${file} CONTENT \"${bat_file_begin}");
for (auto &dp : build_deps)
{
auto &p = dp.second;
// local projects are always built inside solution
if (p.flags[pfLocalProject])
continue;
local.addLine("${bd_" + p.variable_name + "}");
}
local.addLine("${bat_file_error}");
local.decreaseIndent("\")");
local.emptyLines();
local.addLine(R"(
if (ANDROID)
elseif (UNIX)
set(file chmod u+x ${file} COMMAND ${file})
endif()
)");
bool deps = false;
String build_deps_tgt = "${this}";
if (d.empty() && target.find("-b") != target.npos)
{
build_deps_tgt += "-d"; // deps
deps = true;
}
else
build_deps_tgt += "-b-d";
// do not use add_custom_command as it doesn't work
// add custom target and add a dependency below
// second way is to use add custom target + add custom command (POST?(PRE)_BUILD)
local.addLine("set(bp)");
//for (auto &dp : build_deps_all)
for (auto &dp : build_deps)
{
auto &p = dp.second;
// local projects are always built inside solution
if (p.flags[pfLocalProject])
continue;
ScopedDependencyCondition sdc(local, p, false);
local.addLine("set(bp ${bp} ${implib_" + p.variable_name + "})");
local.addLine("set(bp ${bp} ${imploc_" + p.variable_name + "})");
local.addLine("set(bp ${bp} ${impson_" + p.variable_name + "})");
}
local.emptyLines();
local.increaseIndent("add_custom_target(" + build_deps_tgt);
local.addLine("COMMAND ${file}");
local.increaseIndent("BYPRODUCTS ${bp}");
local.decreaseIndent(")", 2);
local.addLine("add_dependencies(${this} " + build_deps_tgt + ")");
print_solution_folder(local, build_deps_tgt, deps ? service_folder : service_deps_folder);
//this causes long paths issue
//if (deps)
// set_target_properties(local, build_deps_tgt, "PROJECT_LABEL", "dependencies");
//else
// set_target_properties(local, build_deps_tgt, "PROJECT_LABEL", (d.flags[pfLocalProject] ? d.ppath.back() : d.target_name) + "-build-dependencies");
local.addLine();
// alias dependencies
if (d.empty())
{
auto tt = "add_dependencies"s;
for (auto &dp : build_deps)
{
if (dp.second.flags[pfLocalProject])
continue;
add_aliases(local, dp.second, false, [&tt](const auto &s, const auto &v)
{
if (v.patch != -1)
return ""s;
return tt + "(" + s + " ${this})";
});
}
}
if (has_build_deps)
ctx += local;
}
ctx.endif();
ctx.addLine();
}
void CMakePrinter::print_copy_dependencies(CMakeEmitter &ctx, const String &target) const
{
config_section_title(ctx, "copy dependencies");
// comment out this part?
const auto cache_cond = !d.empty() && Settings::get_local_settings().build_dir_type != SettingsType::Local;
if (cache_cond)
ctx.if_("CPPAN_USE_CACHE");
// prepare copy files
ctx.addLine("set(ext sh)");
ctx.if_("WIN32");
ctx.addLine("set(ext bat)");
ctx.endif();
ctx.emptyLines();
ctx.addLine("set(file ${BDIR}/cppan_copy_deps_$<CONFIG>.${ext})");
ctx.emptyLines();
ctx.addLine("set(copy_content)");
ctx.if_("WIN32");
ctx.addLine("set(copy_content \"${copy_content} @setlocal\\n\")");
ctx.else_();
ctx.addLine("set(copy_content \"#!/bin/sh\\n\")");
ctx.endif();
// we're in helper, set this var to build target
if (d.empty())
ctx.addLine("set(this " + target + ")");
ctx.emptyLines();
ctx.addLine("set(output_dir ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})");
ctx.if_("NOT output_dir");
ctx.addLine("set(output_dir ${CMAKE_BINARY_DIR})");
ctx.endif();
ctx.if_("VISUAL_STUDIO OR XCODE");
ctx.addLine("set(output_dir ${output_dir}/$<CONFIG>)");
ctx.endif();
ctx.if_("CPPAN_BUILD_OUTPUT_DIR");
ctx.addLine("set(output_dir ${CPPAN_BUILD_OUTPUT_DIR})");
/*ctx.elseif("MSVC");
ctx.addLine("set(output_dir ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})");
ctx.elseif("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
ctx.addLine("set(output_dir ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})");
ctx.else_();
ctx.addLine("set(output_dir ${CMAKE_BINARY_DIR})");*/
ctx.endif();
if (d.flags[pfLocalProject])
ctx.addLine("set(output_dir $<TARGET_FILE_DIR:${this}>)");
ctx.addLine();
ctx.addLine(R"(
set(at_symbol)
if (WIN32)
set(at_symbol @)
endif()
)");
Packages copy_deps;
gather_copy_deps(rd[d].dependencies, copy_deps);
for (auto &dp : copy_deps)
{
auto &p = dp.second;
p.conditions.insert(rd[p].config->getDefaultProject().condition);
if (p.flags[pfExecutable])
{
// if we have an exe, we must include all dependent targets
// because they're not visible from exe directly
ScopedDependencyCondition sdc(ctx, p);
config_section_title(ctx, "Executable build deps for " + p.target_name);
print_dependencies(ctx, p, settings.use_cache);
config_section_title(ctx, "End of executable build deps for " + p.target_name);
ctx.emptyLines();
}
config_section_title(ctx, "Copy " + p.target_name);
ScopedDependencyCondition sdc(ctx, p);
ctx.addLine("set(copy 1)");
ctx.addLine("get_target_property(type " + p.target_name + " TYPE)");
ctx.if_("\"${type}\" STREQUAL STATIC_LIBRARY");
ctx.addLine("set(copy 0)");
ctx.endif();
ctx.addLine();
ctx.if_("CPPAN_COPY_ALL_LIBRARIES_TO_OUTPUT");
ctx.addLine("set(copy 1)");
ctx.endif();
ctx.addLine();
auto prj = rd[p].config->getDefaultProject();
auto output_directory = "${output_dir}/"s;
output_directory += prj.output_directory + "/";
ctx.if_("copy");
{
String s;
s += "set(copy_content \"${copy_content} ${at_symbol}\")\n";
s += " set(copy_content \"${copy_content} \\\"${CMAKE_COMMAND}\\\" -E copy_if_different ";
String name;
if (!prj.output_name.empty())
name = prj.output_name;
else