-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite-cmake.cpp
More file actions
1936 lines (1586 loc) · 58.9 KB
/
write-cmake.cpp
File metadata and controls
1936 lines (1586 loc) · 58.9 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
#include "base64.h"
#include "chibi-internal.h"
#include "filesystem.h"
#include "plistgenerator.h"
#include "stringbuilder.h"
#include <algorithm>
#include <assert.h>
#include <deque>
#include <limits.h> // PATH_MAX
#include <set>
#include <stdarg.h>
#include <string>
#ifdef _MSC_VER
#include <stdlib.h> // _MAX_PATH
#ifndef PATH_MAX
#define PATH_MAX _MAX_PATH
#endif
#endif
#if defined(__GNUC__)
#define sprintf_s(s, ss, f, ...) snprintf(s, ss, f, __VA_ARGS__)
#define vsprintf_s(s, ss, f, a) vsnprintf(s, ss, f, a)
#define strcpy_s(d, ds, s) strcpy(d, s)
#define sscanf_s sscanf
#endif
using namespace chibi;
using namespace chibi_filesystem;
static void report_error(const char * line, const char * format, ...)
{
char text[1024];
va_list ap;
va_start(ap, format);
vsprintf_s(text, sizeof(text), format, ap);
va_end(ap);
//
printf("error: %s\n", text);
}
static const char * translate_toolchain_to_cmake(const std::string & name)
{
if (name == "msvc")
return "MSVC";
return nullptr;
}
static std::string s_platform;
static std::string s_platform_full;
static bool is_platform(const char * platform)
{
if (match_element(s_platform.c_str(), platform, '|'))
return true;
else if (s_platform_full.empty() == false && match_element(s_platform_full.c_str(), platform, '|'))
return true;
else
return false;
}
static std::string always_conditional_begin;
static std::string always_conditional_end;
static std::string dont_makearchive_conditional_begin;
static std::string dont_makearchive_conditional_end;
static std::string makearchive_conditional_begin;
static std::string makearchive_conditional_end;
struct CMakeWriter
{
bool handle_library(const ChibiInfo & chibi_info, ChibiLibrary & library, std::set<std::string> & traversed_libraries, std::vector<ChibiLibrary*> & libraries)
{
#if 0
if (library.isExecutable)
printf("handle_app: %s\n", library.name.c_str());
else
printf("handle_lib: %s\n", library.name.c_str());
#endif
traversed_libraries.insert(library.name);
// recurse library dependencies
for (auto & library_dependency : library.library_dependencies)
{
if (traversed_libraries.count(library_dependency.name) != 0)
continue;
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
ChibiLibrary * found_library = chibi_info.find_library(library_dependency.name.c_str());
if (found_library == nullptr)
{
report_error(nullptr, "failed to find library dependency: %s for target %s", library_dependency.name.c_str(), library.name.c_str());
return false;
}
else
{
if (handle_library(chibi_info, *found_library, traversed_libraries, libraries) == false)
return false;
}
}
else if (library_dependency.type == ChibiLibraryDependency::kType_Local)
{
// nothing to do here
}
else if (library_dependency.type == ChibiLibraryDependency::kType_Find)
{
// nothing to do here
}
else if (library_dependency.type == ChibiLibraryDependency::kType_Global)
{
// nothing to do here
}
else
{
report_error(nullptr, "internal error: unknown library dependency type");
return false;
}
}
libraries.push_back(&library);
return true;
}
template <typename S>
static bool write_app_resource_paths(
const ChibiInfo & chibi_info,
S & sb,
const ChibiLibrary & app,
const std::vector<ChibiLibraryDependency> & library_dependencies)
{
// write a formatted list of all resource paths to CHIBI_RESOURCE_PATHS
{
// for debug/release builds : write the absolute paths to the app/library locations
// this will allow for real-time editing to work, directly from the original source
// locations
StringBuilder resource_paths;
resource_paths.Append("type,name,path\n");
if (app.resource_path.empty() == false)
{
resource_paths.AppendFormat("%s,%s,%s\n",
"app",
app.name.c_str(),
app.resource_path.c_str());
}
for (auto & library_dependency : library_dependencies)
{
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
auto * library = chibi_info.find_library(library_dependency.name.c_str());
if (library->resource_path.empty() == false)
{
resource_paths.AppendFormat("%s,%s,%s\n",
"library",
library->name.c_str(),
library->resource_path.c_str());
}
}
}
const std::string resource_paths_base64 = base64_encode(
resource_paths.text.c_str(),
resource_paths.text.size());
sb.AppendFormat("target_compile_definitions(%s PRIVATE %sCHIBI_RESOURCE_PATHS=\"%s\"%s)\n",
app.name.c_str(),
dont_makearchive_conditional_begin.c_str(),
resource_paths_base64.c_str(),
dont_makearchive_conditional_end.c_str());
sb.Append("\n");
}
{
// for distribution builds : write the relative paths to the app/library locations
// inside the app bundle or package location. this will allow for distributing the
// app with all files located within the same directory structure
StringBuilder resource_paths;
resource_paths.Append("type,name,path\n");
for (auto & library_dependency : library_dependencies)
{
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
auto * library = chibi_info.find_library(library_dependency.name.c_str());
if (library->resource_path.empty() == false)
{
resource_paths.AppendFormat("%s,%s,libs/%s\n",
"library",
library->name.c_str(),
library->name.c_str());
}
}
}
if (s_platform == "windows")
{
resource_paths.AppendFormat("%s,%s,%s\n",
"app",
app.name.c_str(),
"data");
}
const std::string resource_paths_base64 = base64_encode(
resource_paths.text.c_str(),
resource_paths.text.size());
sb.AppendFormat("target_compile_definitions(%s PRIVATE %sCHIBI_RESOURCE_PATHS=\"%s\"%s)\n",
app.name.c_str(),
makearchive_conditional_begin.c_str(),
resource_paths_base64.c_str(),
makearchive_conditional_end.c_str());
sb.Append("\n");
}
return true;
}
template <typename S>
static void write_custom_command_for_distribution(
S & sb,
const char * target,
const char * depends,
const char * command)
{
// note : command may be quite lengthy, so we avoid AppendFormat here
sb.Append("set(args "); sb.Append(command); sb.Append(")\n");
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND %secho%s \"$<1:${args}>\"\n",
target,
dont_makearchive_conditional_begin.c_str(),
dont_makearchive_conditional_end.c_str());
if (depends != nullptr && depends[0] != 0)
sb.AppendFormat("\tDEPENDS %s\n", depends);
sb.Append("\tCOMMAND_EXPAND_LISTS)\n");
sb.Append("unset(args)\n");
sb.Append("\n");
}
template <typename S>
static void write_custom_command_for_distribution_va(
S & sb,
const char * target,
const char * depends,
const char * command_format,
...)
{
char command[1024];
va_list ap;
va_start(ap, command_format);
vsprintf_s(command, sizeof(command), command_format, ap);
va_end(ap);
// note : command may be quite lengthy, so we avoid AppendFormat here
sb.Append("set(args "); sb.Append(command); sb.Append(")\n");
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND %secho%s \"$<1:${args}>\"\n",
target,
dont_makearchive_conditional_begin.c_str(),
dont_makearchive_conditional_end.c_str());
if (depends != nullptr && depends[0] != 0)
sb.AppendFormat("\tDEPENDS %s\n", depends);
sb.Append("\tCOMMAND_EXPAND_LISTS)\n");
sb.Append("unset(args)\n");
sb.Append("\n");
}
template <typename S>
static bool write_copy_resources_for_distribution_using_rsync(S & sb, const ChibiLibrary & app, const ChibiLibrary & library, const char * destination_path)
{
// use rsync to copy resources
// but first make sure the target directory exists
// note : we use a conditional to check if we're building a distribution app bundle
// ideally CMake would have build config dependent custom commands,
// but since it doesn't, we prepend 'echo' to the command, depending on
// whether this is a distribution build or not
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
library.resource_path.c_str(),
"${CMAKE_COMMAND} -E make_directory \"%s\"",
destination_path);
StringBuilder exclude_args;
if (library.resource_excludes.empty() == false)
{
for (auto & exclude : library.resource_excludes)
{
sb.Append("--exclude '");
sb.Append(exclude.c_str());
sb.Append("' ");
}
}
// rsync
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
library.resource_path.c_str(),
"rsync -a %s \"%s/\" \"%s\"",
exclude_args.text.c_str(),
library.resource_path.c_str(),
destination_path);
return true;
}
template <typename S>
static bool write_copy_license_files_for_distribution_using_rsync(
S & sb,
const ChibiLibrary & app,
const ChibiLibrary & library,
const char * destination_path)
{
assert(!library.license_files.empty());
// use rsync to copy the license file(s)
// but first make sure the target directory exists
// note : we use a conditional to check if we're building a distribution app bundle
// ideally CMake would have build config dependent custom commands,
// but since it doesn't, we prepend 'echo' to the command, depending on
// whether this is a distribution build or not
StringBuilder command;
command.AppendFormat(
"${CMAKE_COMMAND} -E make_directory \"%s\"",
destination_path);
write_custom_command_for_distribution(sb,
app.name.c_str(),
nullptr,
command.text.c_str());
command.Reset();
command.Append("rsync\n");
for (auto & license_file : library.license_files)
{
char full_path[PATH_MAX];
if (!concat(full_path, sizeof(full_path), library.path.c_str(), "/", license_file.c_str()))
{
report_error(nullptr, "failed to create absolute path");
return false;
}
command.AppendFormat("\t\"%s\"\n", full_path);
}
command.AppendFormat("\t\"%s\"", destination_path);
write_custom_command_for_distribution(sb,
app.name.c_str(),
destination_path,
command.text.c_str());
return true;
}
template <typename S>
static bool write_header_paths(S & sb, const ChibiLibrary & library)
{
if (!library.header_paths.empty())
{
for (auto & header_path : library.header_paths)
{
const char * visibility = header_path.expose
? "PUBLIC"
: "PRIVATE";
sb.AppendFormat("target_include_directories(%s %s \"%s\")\n",
library.name.c_str(),
visibility,
header_path.alias_through_copy_path.empty() == false
? header_path.alias_through_copy_path.c_str()
: header_path.path.c_str());
}
sb.Append("\n");
}
return true;
}
template <typename S>
static bool write_compile_definitions(S & sb, const ChibiLibrary & library)
{
if (!library.compile_definitions.empty())
{
for (auto & compile_definition : library.compile_definitions)
{
const char * toolchain = translate_toolchain_to_cmake(compile_definition.toolchain);
if (toolchain != nullptr)
{
sb.AppendFormat("if (%s)\n", toolchain);
sb.Append("\t");
}
const char * visibility = compile_definition.expose
? "PUBLIC"
: "PRIVATE";
for (size_t config_index = 0; config_index == 0 || config_index < compile_definition.configs.size(); ++config_index)
{
char condition_begin[64];
char condition_end[64];
if (compile_definition.configs.empty())
{
condition_begin[0] = 0;
condition_end[0] = 0;
}
else
{
if (!concat(condition_begin, sizeof(condition_begin), "$<$<CONFIG:", compile_definition.configs[config_index].c_str(), ">:"))
{
report_error(nullptr, "failed to create compile definition condition");
return false;
}
strcpy_s(condition_end, sizeof(condition_end), ">");
}
if (compile_definition.value.empty())
{
sb.AppendFormat("target_compile_definitions(%s %s %s%s%s)\n",
library.name.c_str(),
visibility,
condition_begin,
compile_definition.name.c_str(),
condition_end);
}
else
{
sb.AppendFormat("target_compile_definitions(%s %s %s%s=%s%s)\n",
library.name.c_str(),
visibility,
condition_begin,
compile_definition.name.c_str(),
compile_definition.value.c_str(),
condition_end);
}
if (toolchain != nullptr)
{
sb.AppendFormat("endif (%s)\n", toolchain);
}
}
}
sb.Append("\n");
}
return true;
}
template <typename S>
static bool write_library_dependencies(S & sb, const ChibiLibrary & library)
{
if (!library.library_dependencies.empty())
{
StringBuilder find;
StringBuilder link;
link.AppendFormat("target_link_libraries(%s", library.name.c_str());
for (auto & library_dependency : library.library_dependencies)
{
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
link.AppendFormat("\n\tPUBLIC %s",
library_dependency.name.c_str());
}
else if (library_dependency.type == ChibiLibraryDependency::kType_Local)
{
link.AppendFormat("\n\tPUBLIC %s",
library_dependency.path.c_str());
}
else if (library_dependency.type == ChibiLibraryDependency::kType_Find)
{
char var_name[256];
if (!concat(var_name, sizeof(var_name), library.name.c_str(), "_", library_dependency.name.c_str()))
{
report_error(nullptr, "failed to construct variable name for 'find' library dependency");
return false;
}
find.AppendFormat("find_library(%s %s)\n", var_name, library_dependency.name.c_str());
link.AppendFormat("\n\tPUBLIC ${%s}",
var_name);
}
else if (library_dependency.type == ChibiLibraryDependency::kType_Global)
{
link.AppendFormat("\n\tPUBLIC %s",
library_dependency.name.c_str());
}
else
{
report_error(nullptr, "internal error: unknown library dependency type");
return false;
}
}
if (!find.text.empty())
{
find.Append("\n");
sb.Append(find.text.c_str());
}
link.Append(")\n");
link.Append("\n");
sb.Append(link.text.c_str());
}
return true;
}
static const char * get_package_dependency_output_name(const std::string & package_dependency)
{
/*
CMake find_package scripts do not always follow the convention of outputting
<package_name>_INCLUDE_DIRS and <package_name>_LIBRARIES. sometimes a script
will capitalize the package name or perhaps something more wild. for now I only
came across the FindFreetype.cmake script which doesn't stick to convention,
but since CMake in no way enforces the convention, other exceptions may exist.
in either case, we normalize known exceptions here since it's bad for automation
to have to deal with these exceptions..
*/
if (package_dependency == "Freetype")
return "FREETYPE";
else if (package_dependency == "OpenGL")
return "OPENGL";
else
return package_dependency.c_str();
}
template <typename S>
static bool write_package_dependencies(S & sb, const ChibiLibrary & library)
{
if (!library.package_dependencies.empty())
{
for (auto & package_dependency : library.package_dependencies)
{
if (package_dependency.type == ChibiPackageDependency::kType_FindPackage)
{
sb.AppendFormat("find_package(%s REQUIRED)\n", package_dependency.name.c_str());
sb.AppendFormat("if (NOT %s_FOUND)\n", package_dependency.name.c_str());
sb.AppendFormat("\tmessage(FATAL_ERROR \"%s not found\")\n", package_dependency.name.c_str());
sb.AppendFormat("endif ()\n");
}
}
#if ENABLE_PKGCONFIG
for (auto & package_dependency : library.package_dependencies)
{
if (package_dependency.type == ChibiPackageDependency::kType_PkgConfig)
{
sb.AppendFormat("pkg_check_modules(%s REQUIRED %s)\n",
package_dependency.variable_name.c_str(),
package_dependency.name.c_str());
}
}
#endif
sb.Append("\n");
for (auto & package_dependency : library.package_dependencies)
{
if (package_dependency.type == ChibiPackageDependency::kType_FindPackage)
{
sb.AppendFormat("target_include_directories(%s PRIVATE \"${%s_INCLUDE_DIRS}\")\n",
library.name.c_str(),
get_package_dependency_output_name(package_dependency.name));
}
#if ENABLE_PKGCONFIG
else if (package_dependency.type == ChibiPackageDependency::kType_PkgConfig)
{
sb.AppendFormat("target_include_directories(%s PRIVATE \"${%s_INCLUDE_DIRS}\")\n",
library.name.c_str(),
package_dependency.variable_name.c_str());
}
#endif
}
sb.Append("\n");
for (auto & package_dependency : library.package_dependencies)
{
if (package_dependency.type == ChibiPackageDependency::kType_FindPackage)
{
sb.AppendFormat("target_link_libraries(%s PRIVATE ${%s_LIBRARIES} ${%s_LIBRARY})\n",
library.name.c_str(),
get_package_dependency_output_name(package_dependency.name),
get_package_dependency_output_name(package_dependency.name));
}
#if ENABLE_PKGCONFIG
else if (package_dependency.type == ChibiPackageDependency::kType_PkgConfig)
{
sb.AppendFormat("target_link_libraries(%s PRIVATE ${%s_LIBRARIES})\n",
library.name.c_str(),
package_dependency.variable_name.c_str());
}
#endif
}
sb.Append("\n");
}
return true;
}
static bool gather_all_library_dependencies(const ChibiInfo & chibi_info, const ChibiLibrary & library, std::vector<ChibiLibraryDependency> & library_dependencies)
{
std::set<std::string> traversed_libraries;
std::deque<const ChibiLibrary*> stack;
stack.push_back(&library);
traversed_libraries.insert(library.name);
while (stack.empty() == false)
{
const ChibiLibrary * library = stack.front();
for (auto & library_dependency : library->library_dependencies)
{
if (traversed_libraries.count(library_dependency.name) == 0)
{
traversed_libraries.insert(library_dependency.name);
library_dependencies.push_back(library_dependency);
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
const ChibiLibrary * resolved_library = chibi_info.find_library(library_dependency.name.c_str());
if (resolved_library == nullptr)
{
report_error(nullptr, "failed to resolve library dependency: %s for library %s", library_dependency.name.c_str(), library->name.c_str());
return false;
}
stack.push_back(resolved_library);
}
}
}
stack.pop_front();
}
return true;
}
static bool write_embedded_app_files(
const ChibiInfo & chibi_info,
StringBuilder & sb,
const ChibiLibrary & app,
const std::vector<ChibiLibraryDependency> & library_dependencies)
{
bool has_embed_dependency = false;
for (auto & library_dependency : library_dependencies)
{
if (library_dependency.embed_framework)
{
has_embed_dependency = true;
// create a custom command where the embedded file(s) are copied into a place where the executable can find it
const char * filename;
auto i = library_dependency.path.find_last_of('/');
if (i == std::string::npos)
filename = library_dependency.path.c_str();
else
filename = &library_dependency.path[i + 1];
if (s_platform == "macos" || s_platform == "iphoneos")
{
if (string_ends_with(filename, ".framework"))
{
// use rsync to recursively copy files if this is a framework
// but first make sure the target directory exists
sb.AppendFormat("set(args ${CMAKE_COMMAND} -E make_directory \"${BUNDLE_PATH}/Contents/Frameworks\")\n");
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND %secho%s \"$<1:${args}>\"\n" \
"\tCOMMAND_EXPAND_LISTS\n" \
"\tDEPENDS \"%s\")\n",
app.name.c_str(),
always_conditional_begin.c_str(),
always_conditional_end.c_str(),
library_dependency.path.c_str());
// rsync
sb.AppendFormat("set(args rsync -a \"%s\" \"${BUNDLE_PATH}/Contents/Frameworks\")\n",
library_dependency.path.c_str());
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND %secho%s \"$<1:${args}>\"\n" \
"\tCOMMAND_EXPAND_LISTS\n" \
"\tDEPENDS \"%s\")\n",
app.name.c_str(),
always_conditional_begin.c_str(),
always_conditional_end.c_str(),
library_dependency.path.c_str());
}
else
{
// just copy the file (if it has changed or doesn't exist)
sb.AppendFormat("set(args ${CMAKE_COMMAND} -E copy_if_different \"%s\" \"${BUNDLE_PATH}/Contents/MacOS/%s\")\n",
library_dependency.path.c_str(),
filename);
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND %secho%s \"$<1:${args}>\"\n" \
"\tCOMMAND_EXPAND_LISTS\n" \
"\tDEPENDS \"%s\")\n",
app.name.c_str(),
always_conditional_begin.c_str(),
always_conditional_end.c_str(),
library_dependency.path.c_str());
}
}
else
{
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND ${CMAKE_COMMAND} -E copy_if_different \"%s\" \"${CMAKE_CURRENT_BINARY_DIR}/%s\"\n" \
"\tDEPENDS \"%s\")\n",
app.name.c_str(),
library_dependency.path.c_str(),
filename,
library_dependency.path.c_str());
}
}
}
if (has_embed_dependency)
sb.Append("\n");
for (auto & library_dependency : library_dependencies)
{
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
const ChibiLibrary * library = chibi_info.find_library(library_dependency.name.c_str());
for (auto & dist_file : library->dist_files)
{
// create a custom command where the generated file(s) are copied into a place where the executable can find it
const char * filename;
auto i = dist_file.find_last_of('/');
if (i == std::string::npos)
filename = dist_file.c_str();
else
filename = &dist_file[i + 1];
if (s_platform == "macos")
{
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND ${CMAKE_COMMAND} -E copy_if_different \"%s\" \"${BUNDLE_PATH}/Contents/MacOS/%s\"\n" \
"\tDEPENDS \"%s\")\n",
app.name.c_str(),
dist_file.c_str(),
filename,
dist_file.c_str());
}
else
{
sb.AppendFormat(
"add_custom_command(\n" \
"\tTARGET %s POST_BUILD\n" \
"\tCOMMAND ${CMAKE_COMMAND} -E copy_if_different \"%s\" \"${CMAKE_CURRENT_BINARY_DIR}/%s\"\n" \
"\tDEPENDS \"%s\")\n",
app.name.c_str(),
dist_file.c_str(),
filename,
dist_file.c_str());
}
}
if (library->shared)
{
// copy generated shared object files into a place where the executable can find it
if (s_platform == "macos")
{
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
library_dependency.name.c_str(),
"${CMAKE_COMMAND} -E copy_if_different\n" \
"\t\"$<TARGET_FILE:%s>\"\n" \
"\t\"${BUNDLE_PATH}/Contents/MacOS/$<TARGET_FILE_NAME:%s>\"",
library_dependency.name.c_str(),
library_dependency.name.c_str());
}
else if (s_platform == "iphoneos")
{
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
library_dependency.name.c_str(),
"${CMAKE_COMMAND} -E copy_if_different\n" \
"\t\"$<TARGET_FILE:%s>\"\n" \
"\t\"${BUNDLE_PATH}/$<TARGET_FILE_NAME:%s>\"",
library_dependency.name.c_str(),
library_dependency.name.c_str());
}
// todo : also copy generated (dll) files on Windows (?)
}
}
}
return true;
}
static bool write_create_windows_app_archive(const ChibiInfo & chibi_info, StringBuilder & sb, const ChibiLibrary & app, const std::vector<ChibiLibraryDependency> & library_dependencies)
{
// create a directory where to copy the executable, distribution and data files
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
nullptr,
"${CMAKE_COMMAND} -E make_directory \"${CMAKE_CURRENT_BINARY_DIR}/%s\"",
app.name.c_str());
// copy the generated executable
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
nullptr,
"${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:%s> \"${CMAKE_CURRENT_BINARY_DIR}/%s\"",
app.name.c_str(),
app.name.c_str());
for (auto & library_dependency : library_dependencies)
{
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
const ChibiLibrary * library = chibi_info.find_library(library_dependency.name.c_str());
// copy generated DLL files
if (library->shared)
{
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
"\"$<TARGET_FILE:%s>\"",
"${CMAKE_COMMAND} -E copy_if_different \"$<TARGET_FILE:%s>\" \"${CMAKE_CURRENT_BINARY_DIR}/%s\"",
library_dependency.name.c_str(),
app.name.c_str());
}
// copy the distribution files
for (auto & dist_file : library->dist_files)
{
// create a custom command where the embedded file(s) are copied into a place where the executable can find it
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
nullptr,
"${CMAKE_COMMAND} -E copy_if_different \"%s\" \"${CMAKE_CURRENT_BINARY_DIR}/%s\"",
dist_file.c_str(),
app.name.c_str());
}
}
}
// copy app resources
if (app.resource_path.empty() == false)
{
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
nullptr,
"${CMAKE_COMMAND} -E make_directory \"${CMAKE_CURRENT_BINARY_DIR}/%s/data\"",
app.name.c_str());
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
nullptr,
"${CMAKE_COMMAND} -E copy_directory \"%s\" \"${CMAKE_CURRENT_BINARY_DIR}/%s/data\"",
app.resource_path.c_str(),
app.name.c_str());
}
// copy library resources
for (auto & library_dependency : library_dependencies)
{
if (library_dependency.type == ChibiLibraryDependency::kType_Generated)
{
auto * library = chibi_info.find_library(library_dependency.name.c_str());
if (library->resource_path.empty() == false)
{
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
nullptr,
"${CMAKE_COMMAND} -E make_directory \"${CMAKE_CURRENT_BINARY_DIR}/%s/data/libs/%s\"",
app.name.c_str(),
library->name.c_str());
write_custom_command_for_distribution_va(sb,
app.name.c_str(),
nullptr,
"${CMAKE_COMMAND} -E copy_directory \"%s\" \"${CMAKE_CURRENT_BINARY_DIR}/%s/data/libs/%s\"",
library->resource_path.c_str(),
app.name.c_str(),
library->name.c_str());
}
}
}
sb.Append("\n");
return true;
}
template <typename S>
static bool output(FILE * f, S & sb)
{
if (fprintf(f, "%s", sb.text.c_str()) < 0)
{
report_error(nullptr, "failed to write to disk");
return false;
}
return true;
}
static void write_set_osx_bundle_path(StringBuilder & sb, const char * app_name)
{
sb.AppendFormat("set(BUNDLE_PATH \"$<TARGET_FILE_DIR:%s>/../..\")\n\n", app_name);
}
static void write_set_ios_bundle_path(StringBuilder & sb, const char * app_name)
{
sb.AppendFormat("set(BUNDLE_PATH \"\\$\\{CONFIGURATION_BUILD_DIR\\}/\\$\\{CONTENTS_FOLDER_PATH\\}\")\n\n", app_name);
}
static bool generate_translation_unit_linkage_files(const ChibiInfo & chibi_info, StringBuilder & sb, const char * generated_path, const std::vector<ChibiLibrary*> & libraries)
{
// generate translation unit linkage files
for (auto * app : libraries)
{
if (app->isExecutable == false)
continue;
std::vector<ChibiLibraryDependency> all_library_dependencies;
if (!gather_all_library_dependencies(chibi_info, *app, all_library_dependencies))
return false;
std::vector<std::string> link_translation_unit_using_function_calls;
for (auto & library_dependency : all_library_dependencies)
{
if (library_dependency.type != ChibiLibraryDependency::kType_Generated)