-
Notifications
You must be signed in to change notification settings - Fork 1
/
capp.cmake
1845 lines (1792 loc) · 72.6 KB
/
capp.cmake
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
cmake_minimum_required(VERSION 3.15...3.21.1)
set(CAPP_TRUE TRUE)
#this regex is designed to match a Git ref so that the remote
#is the first sub-expression and the branch is the second
set(CAPP_REMOTE_REF_REGEX "refs/remotes/([0-9A-Za-z_-]+)/([0-9A-Za-z\\./_-]+)")
if (WIN32)
set(CMAKE_PROGRAM_PATH "C:/Program Files") #workaround a workaround for MSVC 2017 in FindGit.cmake
endif()
find_package(Git REQUIRED QUIET)
find_package(Python COMPONENTS Interpreter QUIET)
function(capp_stdout msg)
execute_process(COMMAND "${CMAKE_COMMAND}" -E echo "${msg}")
endfunction()
function(capp_list_to_string)
cmake_parse_arguments(PARSE_ARGV 0 capp_list_to_string "" "LIST;STRING" "")
set(str)
foreach(item IN LISTS "${capp_list_to_string_LIST}")
if (str)
set(str "${str} ${item}")
else()
set(str "${item}")
endif()
endforeach()
set(${capp_list_to_string_STRING} "${str}" PARENT_SCOPE)
endfunction()
function(capp_get_subdirectories result curdir)
file(GLOB children RELATIVE "${curdir}" "${curdir}/*")
set(dirlist "")
foreach(child ${children})
if(IS_DIRECTORY "${curdir}/${child}")
list(APPEND dirlist ${child})
endif()
endforeach()
set(${result} ${dirlist} PARENT_SCOPE)
endfunction()
function(capp_subdirectories)
cmake_parse_arguments(PARSE_ARGV 0 capp_subdirectories "" "PARENT_DIRECTORY;RESULT_VARIABLE" "")
file(GLOB children RELATIVE "${capp_subdirectories_PARENT_DIRECTORY}" "${capp_subdirectories_PARENT_DIRECTORY}/*")
set(dirlist "")
foreach (child ${children})
if (IS_DIRECTORY "${capp_subdirectories_PARENT_DIRECTORY}/${child}")
list(APPEND dirlist ${child})
endif()
endforeach()
set(${capp_subdirectories_RESULT_VARIABLE} ${dirlist} PARENT_SCOPE)
endfunction()
function(capp_execute)
cmake_parse_arguments(PARSE_ARGV
0
capp_execute
"OUTPUT_QUIET;ERROR_QUIET;OUTPUT_STRIP_TRAILING_WHITESPACE"
"WORKING_DIRECTORY;RESULT_VARIABLE;OUTPUT_VARIABLE;ERROR_VARIABLE"
"COMMAND")
set(extra_args)
if (capp_execute_OUTPUT_VARIABLE)
list(APPEND extra_args OUTPUT_VARIABLE capp_execute_output)
endif()
if (capp_execute_ERROR_VARIABLE)
list(APPEND extra_args ERROR_VARIABLE capp_execute_error)
endif()
if (capp_execute_OUTPUT_QUIET)
list(APPEND extra_args OUTPUT_QUIET)
endif()
if (capp_execute_ERROR_QUIET)
list(APPEND extra_args ERROR_QUIET)
endif()
if (capp_execute_OUTPUT_STRIP_TRAILING_WHITESPACE)
list(APPEND extra_args OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
execute_process(
COMMAND ${capp_execute_COMMAND}
WORKING_DIRECTORY "${capp_execute_WORKING_DIRECTORY}"
RESULT_VARIABLE capp_execute_result
${extra_args}
)
if (capp_execute_OUTPUT_VARIABLE)
set(${capp_execute_OUTPUT_VARIABLE} "${capp_execute_output}" PARENT_SCOPE)
endif()
if (capp_execute_ERROR_VARIABLE)
set(${capp_execute_ERROR_VARIABLE} "${capp_execute_error}" PARENT_SCOPE)
endif()
set(${capp_execute_RESULT_VARIABLE} "${capp_execute_result}" PARENT_SCOPE)
endfunction()
function(capp_add_paths result var list)
string(REPLACE ":" ";" contents "$ENV{${var}}")
foreach(path IN LISTS list)
if (NOT "${path}" IN_LIST contents)
list(PREPEND contents "${path}")
endif()
endforeach()
string(REPLACE ";" ":" contents "${contents}")
set(${result} "${contents}" PARENT_SCOPE)
endfunction()
function(capp_remove_paths result var list)
string(REPLACE ":" ";" contents "$ENV{${var}}")
foreach(path IN LISTS list)
list(REMOVE_ITEM contents "${path}")
endforeach()
string(REPLACE ";" ":" contents "${contents}")
set(${result} "${contents}" PARENT_SCOPE)
endfunction()
function(capp_add_file)
cmake_parse_arguments(PARSE_ARGV 0 capp_add_file "" "RESULT_VARIABLE;FILE" "")
capp_execute(
COMMAND "${GIT_EXECUTABLE}" add "${capp_add_file_FILE}"
WORKING_DIRECTORY "${CAPP_ROOT}"
RESULT_VARIABLE git_add_result
)
set(${capp_add_file_RESULT_VARIABLE} ${git_add_result} PARENT_SCOPE)
endfunction()
function(capp_commit)
cmake_parse_arguments(PARSE_ARGV 0 capp_commit "" "RESULT_VARIABLE;MESSAGE" "")
capp_execute(
COMMAND "${GIT_EXECUTABLE}" commit -m "${capp_commit_MESSAGE}"
WORKING_DIRECTORY "${CAPP_ROOT}"
RESULT_VARIABLE git_commit_result
)
set(${capp_commit_RESULT_VARIABLE} ${git_commit_result} PARENT_SCOPE)
endfunction()
function(capp_changes_committed)
cmake_parse_arguments(PARSE_ARGV 0 capp_changes_committed "" "PACKAGE;RESULT_VARIABLE;OUTPUT_VARIABLE" "")
set(package ${capp_changes_committed_PACKAGE})
set(result_variable ${capp_changes_committed_RESULT_VARIABLE})
set(output_variable ${capp_changes_committed_OUTPUT_VARIABLE})
if (${package}_IGNORE_UNCOMMITTED)
set(${result_variable} 0 PARENT_SCOPE)
return()
endif()
capp_execute(
COMMAND "${GIT_EXECUTABLE}" status --porcelain
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
RESULT_VARIABLE git_uncommitted_result
OUTPUT_VARIABLE git_uncommitted_output)
if (NOT git_uncommitted_result EQUAL 0)
set(${result_variable} ${git_uncommitted_result} PARENT_SCOPE)
return()
endif()
if (git_uncommitted_output)
set(${output_variable} "${git_uncommitted_output}" PARENT_SCOPE)
set(${result_variable} -1 PARENT_SCOPE)
return()
endif()
set(${result_variable} 0 PARENT_SCOPE)
endfunction()
#This function's main purpose is to get the repository at source/<package>
#to be at the commit specified in package/<package>/package.cmake
#(technically, whatever is already in ${package}_COMMIT and ${package}_GIT_URL)
#It also aims to do this as nicely as possible.
#For example, if that commit is the head of a branch, then the repository
#should be at that branch and tracking the appropriate remote branch.
function(capp_checkout)
cmake_parse_arguments(PARSE_ARGV 0 capp_checkout "" "PACKAGE;RESULT_VARIABLE" "")
set(package ${capp_checkout_PACKAGE})
set(desired_commit ${${package}_COMMIT})
set(desired_git_url ${${package}_GIT_URL})
set(has_submodules ${${package}_HAS_SUBMODULES})
#Safety check: make sure there are not uncommitted changes, otherwise the
#package can't really be checked out to the desired commit.
capp_changes_committed(
PACKAGE ${package}
RESULT_VARIABLE uncommitted_result
OUTPUT_VARIABLE uncommitted_output)
if (NOT uncommitted_result EQUAL 0)
message("\nCApp refusing to check out ${package} because it has uncommitted changes:\n${uncommitted_output}")
set(${capp_checkout_RESULT_VARIABLE} -1 PARENT_SCOPE)
return()
endif()
#The common case is that the repository is already
#at this commit. In that case, just exit early.
capp_get_package_commit(
PACKAGE ${package}
COMMIT_VARIABLE current_commit
RESULT_VARIABLE get_commit_result
)
if (NOT get_commit_result EQUAL 0)
message("CApp: failed to get the current commit of ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${get_commit_result} PARENT_SCOPE)
return()
endif()
if (current_commit STREQUAL desired_commit)
message("CApp: ${package} is already at the desired commit")
else()
#From here on out we will be trying to change the commit that a package
#is checked out to, so we will mark it as needing re-configuration
capp_invalidate_config(${package})
#For multiple reasons (either fetching or picking a branch to check out),
#we need to identify which remote the desired URL maps to.
set(remote)
#In order to do that, we begin by gathering a list of existing remotes.
capp_execute(
COMMAND "${GIT_EXECUTABLE}" remote show
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
RESULT_VARIABLE remote_show_result
OUTPUT_VARIABLE remote_show_output)
if (NOT remote_show_result EQUAL 0)
message("git remote show failed on ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${remote_show_result} PARENT_SCOPE)
return()
endif()
#Replace any newlines with a semicolon,
#thus converting a one-line-per-remote string into a CMake list
string(REGEX REPLACE "[\r\n]+" ";" existing_remotes "${remote_show_output}")
list(REMOVE_ITEM existing_remotes "")
message("CApp: in ${package}, existing_remotes=${existing_remotes}")
#Then we check if any of these remotes match the desired URL:
foreach(existing_remote IN LISTS existing_remotes)
capp_get_remote_url(
PACKAGE ${package}
REMOTE ${existing_remote}
GIT_URL_VARIABLE existing_remote_url
RESULT_VARIABLE remote_url_result)
if (NOT remote_show_result EQUAL 0)
message("failed to get URL for remote ${remote} of ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${remote_url_result} PARENT_SCOPE)
return()
endif()
message("CApp: ${package} remote ${existing_remote} has URL ${existing_remote_url}")
if (existing_remote_url STREQUAL desired_git_url)
message("CApp: ${package} remote ${existing_remote} matches desired URL ${desired_git_url}")
set(remote ${existing_remote})
endif()
endforeach()
if (NOT remote)
#If we are here, then none of the existing remotes match the desired Git URL.
#In that case, CApp will go so far as to try to add it for you with a reasonable name.
#That reasonable name will be essentially the GitHub/GitLab organization/group path.
#The following crazy regex is just trying to extract the first component of the
#remote repository path.
string(REGEX REPLACE
".*(:|\\.[a-z]+/)([A-Za-z0-9_-]+)/[A-Za-z0-9/_-]+(\\.git)?"
"\\2"
reasonable_remote_name
"${desired_git_url}")
message("CApp: a reasonable name for the remote for ${desired_git_url} is ${reasonable_remote_name}")
if (reasonable_remote_name IN_LIST existing_remotes)
#If this reasonable name is already one of the remotes, let's just give up.
#It is best for the user to decide how to resolve this mess.
message("\nCApp wanted to add a remote ${reasonable_remote_name} with URL ${desired_git_url} to ${package} but it already exists.\n")
set(${capp_checkout_RESULT_VARIABLE} -1 PARENT_SCOPE)
return()
endif()
#We have a reasonable name and it isn't one of the remotes yet, let's add it
capp_execute(
COMMAND "${GIT_EXECUTABLE}" remote add ${reasonable_remote_name} ${desired_git_url}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
RESULT_VARIABLE remote_add_result)
if (NOT remote_add_result EQUAL 0)
message("failed to add remote ${reasonable_remote_name} with URL ${desired_git_url} to ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${remote_add_result} PARENT_SCOPE)
return()
endif()
message("CApp: added remote ${reasonable_remote_name} with URL ${desired_git_url} to ${package}")
set(remote ${reasonable_remote_name})
endif()
#If we are here, then the repository is not at the desired commit.
#Performance optimization: check to see if the commit exists in the
#local repository first before going to the fetching step, because
#fetching may not be possible in some cases (in which case we still
#want this operation to succeed if it can) and is expensive.
capp_execute(
COMMAND "${GIT_EXECUTABLE}" cat-file -e ${desired_commit}^{commit}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
OUTPUT_QUIET
ERROR_QUIET
RESULT_VARIABLE commit_exists_result)
if (NOT commit_exists_result EQUAL 0)
#If we are here, then the desired commit doesn't exist locally.
#In that case, the next step
#is to ensure the desired Git URL exists as a remote and has been fetched.
message("CApp: ${package} desired commit ${desired_commit} doesn't exist locally")
#If we are here, then ${remote} is a remote with the right Git URL.
#Let's fetch it.
set(shallow_failed FALSE)
message("CApp: shallow fetching refs of package ${package}")
capp_execute(
COMMAND "${GIT_EXECUTABLE}" fetch --depth 1 ${remote}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
OUTPUT_QUIET
ERROR_QUIET
RESULT_VARIABLE refs_fetch_result)
if (NOT refs_fetch_result EQUAL 0)
message("CApp: failed to shallow fetch refs of ${package}")
set(shallow_failed TRUE)
endif()
message("CApp: shallow fetching commit ${desired_commit} of ${package}")
capp_execute(
COMMAND "${GIT_EXECUTABLE}" fetch --depth 1 ${remote}
${desired_commit}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
OUTPUT_QUIET
ERROR_QUIET
RESULT_VARIABLE commit_fetch_result)
if (NOT commit_fetch_result EQUAL 0)
message("CApp: failed to shallow fetch commit ${desired_commit} of ${package}")
set(shallow_failed TRUE)
endif()
if (shallow_failed)
message("CApp: failed to shallow fetch ${package}")
message("CApp: fully fetching remote ${remote} of ${package}")
capp_execute(
COMMAND "${GIT_EXECUTABLE}" fetch ${remote}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
OUTPUT_QUIET
ERROR_QUIET
RESULT_VARIABLE fetch_result)
if (NOT fetch_result EQUAL 0)
message("CApp: failed to fetch remote ${remote} of ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${fetch_result} PARENT_SCOPE)
return()
endif()
endif()
message("CApp: succeeded in fetching remote ${remote} of ${package}")
#Then let's check if the given commit exists now.
capp_execute(
COMMAND "${GIT_EXECUTABLE}" cat-file -e ${desired_commit}^{commit}
OUTPUT_QUIET
ERROR_QUIET
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
RESULT_VARIABLE commit_exists_result)
if (NOT commit_exists_result EQUAL 0)
#If it doesn't, then this is user error and the desired commit isn't in the
#desired Git URL.
message("\nCApp can't find commit ${desired_commit} of ${package} even after fetching from ${desired_git_url}\n")
set(${capp_checkout_RESULT_VARIABLE} -1 PARENT_SCOPE)
return()
endif()
endif()
#If we're here, then the desired commit does exist in the local Git repository
#for the package.
#Now, we begin the process of checking it out as nicely as possible.
#The main theme of this niceness is to check out a branch if possible.
#So, the first step is to see if any remote branches (actualy refs) on the desired
#remote point to the desired commit.
capp_execute(
COMMAND "${GIT_EXECUTABLE}" for-each-ref --points-at=${desired_commit}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
OUTPUT_VARIABLE pointing_refs_output
RESULT_VARIABLE pointing_refs_result)
if (NOT pointing_refs_result EQUAL 0)
message("git for-each-ref --points-at=${desired_commit} failed for ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${pointing_refs_result} PARENT_SCOPE)
return()
endif()
#Use MATCHALL to extract a CMake list of all the remote refs that point to the
#desired commit.
string(REGEX MATCHALL "refs/remotes/${remote}/[^ \t\r\n]+" pointing_refs "${pointing_refs_output}")
#One of these refs can be HEAD, which is special and not a branch name
list(REMOVE_ITEM pointing_refs "refs/remotes/${remote}/HEAD")
message("CApp: in ${package}, pointing_refs=${pointing_refs}")
if (pointing_refs)
#At this point, there are some remote refs that point to the desired commit.
#Let's try to pick one of those as the branch to check out.
#Honestly, I can't think of much more than preferring the typical default
#branch names and after that just choosing the first one on the list.
if ("refs/remotes/${remote}/master" IN_LIST pointing_refs)
set(branch "master")
elseif ("refs/remotes/${remote}/main" IN_LIST pointing_refs)
set(branch "main")
else()
list(GET pointing_refs 0 pointing_ref)
string(REGEX REPLACE "${CAPP_REMOTE_REF_REGEX}" "\\2" branch "${pointing_ref}")
endif()
message("CApp: in ${package}, picked branch ${branch}")
#Okay, we finally have a remote and a branch that we want to check out.
#Let's do it!
capp_execute(
COMMAND "${GIT_EXECUTABLE}" checkout -B ${branch} --track ${remote}/${branch}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
OUTPUT_QUIET
ERROR_QUIET
RESULT_VARIABLE branch_checkout_result)
if (NOT branch_checkout_result EQUAL 0)
message("git checkout -B ${branch} --track ${remote}/${branch} failed in ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${branch_checkout_result} PARENT_SCOPE)
return()
endif()
message("CApp: in ${package}, checked out ${branch}, tracking ${remote}/${branch}")
else()
#If there are no remote refs that point to this commit, then it is a
#"detached HEAD" situation and we can just check out the desired commit.
capp_execute(
COMMAND "${GIT_EXECUTABLE}" checkout ${desired_commit}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
OUTPUT_QUIET
ERROR_QUIET
RESULT_VARIABLE detached_checkout_result)
if (NOT detached_checkout_result EQUAL 0)
message("git checkout ${desired_commit} failed in ${package}")
set(${capp_checkout_RESULT_VARIABLE} ${detached_checkout_result} PARENT_SCOPE)
return()
endif()
message("CApp: checked out commit ${desired_commit} explicitly")
endif()
endif()
if(has_submodules)
#Aaaand by now we've succeeded in "git checkout"'ing a good thing.
#But we're not done yet! Submodules!
capp_execute(
COMMAND "${GIT_EXECUTABLE}" submodule update --init --recursive
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}"
RESULT_VARIABLE submodule_result
)
if (NOT submodule_result EQUAL 0)
message("git submodule update --init --recursive failed")
set(${capp_checkout_RESULT_VARIABLE} "${submodule_result}" PARENT_SCOPE)
return()
endif()
message("CApp: submodule update of ${package} completed")
endif()
message("CApp: checkout of ${package} succeeded")
#We did it. We "checked out a commit".
set(${capp_checkout_RESULT_VARIABLE} 0 PARENT_SCOPE)
endfunction()
function(capp_clone)
cmake_parse_arguments(PARSE_ARGV 0 capp_clone "" "PACKAGE;RESULT_VARIABLE" "")
file(MAKE_DIRECTORY "${CAPP_SOURCE_ROOT}")
set(cmd_list
"${GIT_EXECUTABLE}"
clone --depth 1
"${${capp_clone_PACKAGE}_GIT_URL}"
${capp_clone_PACKAGE})
message("\nCApp: shallow cloning ${capp_clone_PACKAGE} from ${${capp_clone_PACKAGE}_GIT_URL}\n")
capp_execute(
COMMAND ${cmd_list}
WORKING_DIRECTORY "${CAPP_SOURCE_ROOT}"
RESULT_VARIABLE git_clone_result
OUTPUT_QUIET
)
if (NOT git_clone_result EQUAL 0)
capp_list_to_string(LIST cmd_list STRING cmd_string)
message("\nCApp: shallow clone of ${capp_clone_PACKAGE} failed.\nCommand was: ${cmd_string}\n")
file(REMOVE_RECURSE "${CAPP_SOURCE_ROOT}/${capp_clone_PACKAGE}")
set(${capp_clone_RESULT_VARIABLE} "${git_clone_result}" PARENT_SCOPE)
return()
endif()
capp_checkout(
PACKAGE ${capp_clone_PACKAGE}
RESULT_VARIABLE checkout_result)
if (NOT checkout_result EQUAL 0)
message("\nCApp: checkout of ${capp_clone_PACKAGE} failed\n")
set(${capp_clone_RESULT_VARIABLE} "${checkout_result}" PARENT_SCOPE)
return()
endif()
message("CApp: clone of ${capp_clone_PACKAGE} succeeded\n")
set(${capp_clone_RESULT_VARIABLE} 0 PARENT_SCOPE)
set(${capp_clone_PACKAGE}_IS_CLONED TRUE PARENT_SCOPE)
endfunction()
function(capp_ensure_venv)
if (NOT EXISTS "${CAPP_VENV_ROOT}")
message("CApp: Python virtual environment at ${CAPP_VENV_ROOT} is needed but doesn't exist")
if (NOT Python_FOUND)
message(FATAL_ERROR "CApp: Cannot create Python virtual environment because Python was not found")
endif()
message("CApp: Creating new Python virtual environment at ${CAPP_VENV_ROOT} using ${Python_EXECUTABLE}")
set(cmd_list
"${Python_EXECUTABLE}"
-m
venv
"${CAPP_VENV_ROOT}"
)
capp_execute(
COMMAND ${cmd_list}
RESULT_VARIABLE venv_result
)
if (NOT venv_result EQUAL 0)
capp_list_to_string(LIST cmd_list STRING cmd_string)
file(REMOVE_RECURSE "${CAPP_VENV_ROOT}")
message(FATAL_ERROR "CApp: Failed to create Python virtual environment.\ncommand: ${cmd_string}\n")
else()
message("CApp: Successfully created Python virtual environment at ${CAPP_VENV_ROOT}")
endif()
set(cmd_list
"${CAPP_VENV_ROOT}/bin/pip"
install
${CAPP_PIP_FLAGS}
--upgrade
wheel
setuptools
pip
)
capp_list_to_string(LIST cmd_list STRING cmd_string)
capp_execute(
COMMAND ${cmd_list}
RESULT_VARIABLE pip_result
)
if (NOT ${pip_result} EQUAL 0)
file(REMOVE_RECURSE "${CAPP_VENV_ROOT}")
message(FATAL_ERROR "CApp: Failed to upgrade wheel, setuptools, and pip in Python virtual environment.\nThis can be due to proxy issues, try passing the --proxy flag to CApp.\ncommand: ${cmd_string}\n")
return()
else()
message("CApp: Successfully upgraded wheel, setuptools, and pip in Python virtual environment")
endif()
endif()
endfunction()
function(capp_configure)
cmake_parse_arguments(PARSE_ARGV 0 capp_configure "" "PACKAGE;RESULT_VARIABLE" "")
if (${capp_configure_PACKAGE}_PYTHON_DEPENDENCIES)
capp_ensure_venv()
capp_list_to_string(LIST ${capp_configure_PACKAGE}_PYTHON_DEPENDENCIES STRING depstring)
message("CApp: Installing Python dependencies ${depstring} of ${capp_configure_PACKAGE} using pip")
set(cmd_list
"${CAPP_VENV_ROOT}/bin/pip"
install
${CAPP_PIP_FLAGS}
${${capp_configure_PACKAGE}_PYTHON_DEPENDENCIES}
)
capp_list_to_string(LIST cmd_list STRING cmd_string)
capp_execute(
COMMAND ${cmd_list}
RESULT_VARIABLE pip_result
)
if (NOT ${pip_result} EQUAL 0)
set(${capp_configure_RESULT_VARIABLE} ${pip_result} PARENT_SCOPE)
message("CApp: failed to install Python dependencies of ${capp_configure_PACKAGE}\ncommand: ${cmd_string}\n")
return()
else()
message("CApp: successfully installed Python dependencies ${depstring} of ${capp_configure_PACKAGE}")
endif()
endif()
file(MAKE_DIRECTORY "${CAPP_BUILD_ROOT}/${capp_configure_PACKAGE}")
set(source_directory "${CAPP_SOURCE_ROOT}/${capp_configure_PACKAGE}")
if (${capp_configure_PACKAGE}_SUBDIRECTORY)
set(source_directory "${source_directory}/${${capp_configure_PACKAGE}_SUBDIRECTORY}")
endif()
set(cmakelists_path "${source_directory}/CMakeLists.txt")
set(setup_path "${source_directory}/setup.py")
set(pyproject_path "${source_directory}/pyproject.toml")
if (EXISTS "${cmakelists_path}")
set(options "-DCMAKE_INSTALL_PREFIX=${CAPP_INSTALL_ROOT}/${capp_configure_PACKAGE}")
if (NOT WIN32)
list(APPEND options "-DCMAKE_BUILD_TYPE=${${capp_configure_PACKAGE}_BUILD_TYPE}")
endif()
list(APPEND options ${${capp_configure_PACKAGE}_OPTIONS})
capp_list_to_string(LIST options STRING print_options)
message("\nCApp configuring ${capp_configure_PACKAGE} with these options: ${print_options}\n")
capp_execute(
COMMAND
"${CMAKE_COMMAND}"
"${source_directory}"
${options}
WORKING_DIRECTORY "${CAPP_BUILD_ROOT}/${capp_configure_PACKAGE}"
RESULT_VARIABLE cmake_configure_result
)
set(${capp_configure_RESULT_VARIABLE} "${cmake_configure_result}" PARENT_SCOPE)
if (cmake_configure_result EQUAL 0)
set(${capp_configure_PACKAGE}_IS_CONFIGURED TRUE PARENT_SCOPE)
file(WRITE "${CAPP_BUILD_ROOT}/${capp_configure_PACKAGE}/capp_configured.txt" "Yes")
endif()
elseif(EXISTS "${setup_path}" OR EXISTS "${pyproject_path}")
set(${capp_configure_RESULT_VARIABLE} 0 PARENT_SCOPE)
set(${capp_configure_PACKAGE}_IS_CONFIGURED TRUE PARENT_SCOPE)
file(WRITE "${CAPP_BUILD_ROOT}/${capp_configure_PACKAGE}/capp_configured.txt" "Yes")
else()
set(${capp_configure_RESULT_VARIABLE} -1 PARENT_SCOPE)
message("\nCApp: none of the following exist:\n${cmakelists_path}\n${setup_path}\n${pyproject_path}\n")
endif()
endfunction()
function(capp_build)
cmake_parse_arguments(PARSE_ARGV 0 capp_build "" "PACKAGE;RESULT_VARIABLE" "ARGUMENTS")
set(with_args "")
if (capp_build_ARGUMENTS)
capp_list_to_string(LIST capp_build_ARGUMENTS STRING print_args)
set(with_args " with extra arguments ${print_args}")
endif()
message("\nCApp building ${capp_build_PACKAGE}${with_args}\n")
set(source_directory "${CAPP_SOURCE_ROOT}/${capp_build_PACKAGE}")
if (${capp_build_PACKAGE}_SUBDIRECTORY)
set(source_directory "${source_directory}/${${capp_build_PACKAGE}_SUBDIRECTORY}")
endif()
set(cmakelists_path "${source_directory}/CMakeLists.txt")
if (EXISTS "${cmakelists_path}")
capp_execute(
COMMAND
"${CMAKE_COMMAND}"
"--build"
"."
"--config"
${${capp_build_PACKAGE}_BUILD_TYPE}
${capp_build_ARGUMENTS}
WORKING_DIRECTORY "${CAPP_BUILD_ROOT}/${capp_build_PACKAGE}"
RESULT_VARIABLE cmake_build_result
)
set(${capp_build_RESULT_VARIABLE} "${cmake_build_result}" PARENT_SCOPE)
else()
set(${capp_build_RESULT_VARIABLE} 0 PARENT_SCOPE)
endif()
endfunction()
function(capp_install)
cmake_parse_arguments(PARSE_ARGV 0 capp_install "" "PACKAGE;RESULT_VARIABLE" "")
message("\nCApp installing ${capp_install_PACKAGE}\n")
set(source_directory "${CAPP_SOURCE_ROOT}/${capp_install_PACKAGE}")
if (${capp_install_PACKAGE}_SUBDIRECTORY)
set(source_directory "${source_directory}/${${capp_install_PACKAGE}_SUBDIRECTORY}")
endif()
set(cmakelists_path "${source_directory}/CMakeLists.txt")
set(setup_path "${source_directory}/setup.py")
set(pyproject_path "${source_directory}/pyproject.toml")
if (EXISTS "${cmakelists_path}")
capp_execute(
COMMAND
"${CMAKE_COMMAND}"
"--install"
"."
"--config"
${${capp_install_PACKAGE}_BUILD_TYPE}
WORKING_DIRECTORY "${CAPP_BUILD_ROOT}/${capp_install_PACKAGE}"
RESULT_VARIABLE cmake_install_result
)
set(${capp_install_RESULT_VARIABLE} "${cmake_install_result}" PARENT_SCOPE)
elseif(EXISTS "${setup_path}" OR EXISTS "${pyproject_path}")
capp_ensure_venv()
message("CApp: Installing ${capp_install_PACKAGE} using pip")
set(cmd_list
"${CAPP_VENV_ROOT}/bin/pip"
install
${CAPP_PIP_FLAGS}
"${source_directory}"
)
capp_list_to_string(LIST cmd_list STRING cmd_string)
capp_execute(
COMMAND ${cmd_list}
RESULT_VARIABLE pip_result
)
set(${capp_install_RESULT_VARIABLE} ${pip_result} PARENT_SCOPE)
if (NOT pip_result EQUAL 0)
message("CApp: failed to install ${capp_install_PACKAGE} with pip\ncommand: ${cmd_string}\n")
endif()
else()
set(${capp_install_RESULT_VARIABLE} -1 PARENT_SCOPE)
message("CApp: none of the following exist:\n${cmakelists_path}\n${setup_path}")
endif()
endfunction()
macro(capp_app)
cmake_parse_arguments(capp_app "" "BUILD_TYPE" "ROOT_PACKAGES" ${ARGN})
set(CAPP_ROOT_PACKAGES "${capp_app_ROOT_PACKAGES}")
set(build_type "${capp_app_BUILD_TYPE}")
if (NOT build_type)
set(build_type Release)
endif()
if (NOT CAPP_BUILD_TYPE)
set(CAPP_BUILD_TYPE "${build_type}")
endif()
endmacro()
function(capp_package)
cmake_parse_arguments(PARSE_ARGV 0 arg
"NO_CONFIGURE_CACHE;IGNORE_UNCOMMITTED;HAS_SUBMODULES;IS_LOCAL"
"GIT_URL;COMMIT;SUBDIRECTORY;BUILD_TYPE"
"OPTIONS;DEPENDENCIES;PYTHON_DEPENDENCIES;PYTHONPATH")
set(${CAPP_PACKAGE}_GIT_URL ${arg_GIT_URL} PARENT_SCOPE)
set(${CAPP_PACKAGE}_COMMIT ${arg_COMMIT} PARENT_SCOPE)
set(${CAPP_PACKAGE}_OPTIONS "${arg_OPTIONS}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_DEPENDENCIES "${arg_DEPENDENCIES}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_PYTHON_DEPENDENCIES "${arg_PYTHON_DEPENDENCIES}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_PYTHONPATH "${arg_PYTHONPATH}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_NO_CONFIGURE_CACHE "${arg_NO_CONFIGURE_CACHE}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_IGNORE_UNCOMMITTED "${arg_IGNORE_UNCOMMITTED}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_HAS_SUBMODULES "${arg_HAS_SUBMODULES}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_IS_LOCAL "${arg_IS_LOCAL}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_SUBDIRECTORY "${arg_SUBDIRECTORY}" PARENT_SCOPE)
if (arg_BUILD_TYPE)
set(${CAPP_PACKAGE}_BUILD_TYPE "${arg_BUILD_TYPE}" PARENT_SCOPE)
else()
set(${CAPP_PACKAGE}_BUILD_TYPE "${CAPP_BUILD_TYPE}" PARENT_SCOPE)
endif()
endfunction()
function(capp_topsort_packages)
set(unsorted_list "${CAPP_PACKAGES}")
set(sorted_list)
set(no_incoming_set)
foreach (package IN LISTS unsorted_list)
set(${package}_dependers)
endforeach()
foreach (package IN LISTS unsorted_list)
set(${package}_dependees "${${package}_DEPENDENCIES}")
foreach (dependee IN LISTS ${package}_dependees)
list(APPEND ${dependee}_dependers ${package})
endforeach()
if (NOT ${package}_dependees)
list(APPEND no_incoming_set ${package})
endif()
endforeach()
while (no_incoming_set)
list(POP_FRONT no_incoming_set package_n)
list(APPEND sorted_list ${package_n})
while (${package_n}_dependers)
list(POP_FRONT ${package_n}_dependers depender)
list(REMOVE_ITEM ${depender}_dependees ${package_n})
if (NOT ${depender}_dependees)
list(APPEND no_incoming_set ${depender})
list(REMOVE_DUPLICATES no_incoming_set)
endif()
endwhile()
endwhile()
set(bad_edges)
foreach (package IN LISTS unsorted_list)
foreach (dependee IN LISTS ${package}_dependees)
set(bad_edges "${bad_edges}${dependee} -> ${package}\n")
endforeach()
endforeach()
if (bad_edges)
message(FATAL_ERROR "There is a cycle in the dependency graph involving:\n${bad_edges}")
endif()
set(CAPP_PACKAGES "${sorted_list}" PARENT_SCOPE)
endfunction()
function(capp_build_install)
cmake_parse_arguments(PARSE_ARGV 0 capp_build_install "" "PACKAGE;RESULT_VARIABLE" "BUILD_ARGUMENTS")
capp_build(
PACKAGE ${capp_build_install_PACKAGE}
ARGUMENTS
${capp_build_install_BUILD_ARGUMENTS}
RESULT_VARIABLE capp_build_result
)
if (NOT capp_build_result EQUAL 0)
set(${capp_build_install_RESULT_VARIABLE} ${capp_build_result} PARENT_SCOPE)
message("CApp: capp_build_install failed because capp_build failed")
return()
endif()
capp_install(
PACKAGE ${capp_build_install_PACKAGE}
RESULT_VARIABLE capp_install_result
)
set(${capp_build_install_RESULT_VARIABLE} ${capp_install_result} PARENT_SCOPE)
if (capp_install_result EQUAL 0)
set(${capp_build_install_PACKAGE}_IS_INSTALLED TRUE PARENT_SCOPE)
file(WRITE "${CAPP_BUILD_ROOT}/${capp_build_install_PACKAGE}/capp_installed.txt" "Yes")
else()
message("CApp: capp_build_install failed because capp_install failed")
endif()
endfunction()
function(capp_read_package_file)
cmake_parse_arguments(PARSE_ARGV 0 capp_read_package_file "" "PACKAGE" "")
set(CAPP_PACKAGE ${capp_read_package_file_PACKAGE})
set(capp_read_package_file_path "${CAPP_PACKAGE_ROOT}/${CAPP_PACKAGE}/package.cmake")
include("${capp_read_package_file_path}")
set(${CAPP_PACKAGE}_NO_CONFIGURE_CACHE ${${CAPP_PACKAGE}_NO_CONFIGURE_CACHE} PARENT_SCOPE)
set(${CAPP_PACKAGE}_IGNORE_UNCOMMITTED ${${CAPP_PACKAGE}_IGNORE_UNCOMMITTED} PARENT_SCOPE)
set(${CAPP_PACKAGE}_HAS_SUBMODULES ${${CAPP_PACKAGE}_HAS_SUBMODULES} PARENT_SCOPE)
set(${CAPP_PACKAGE}_IS_LOCAL ${${CAPP_PACKAGE}_IS_LOCAL} PARENT_SCOPE)
set(${CAPP_PACKAGE}_GIT_URL ${${CAPP_PACKAGE}_GIT_URL} PARENT_SCOPE)
set(${CAPP_PACKAGE}_COMMIT ${${CAPP_PACKAGE}_COMMIT} PARENT_SCOPE)
set(${CAPP_PACKAGE}_OPTIONS "${${CAPP_PACKAGE}_OPTIONS}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_DEPENDENCIES "${${CAPP_PACKAGE}_DEPENDENCIES}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_PYTHON_DEPENDENCIES "${${CAPP_PACKAGE}_PYTHON_DEPENDENCIES}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_PYTHONPATH "${${CAPP_PACKAGE}_PYTHONPATH}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_SUBDIRECTORY "${${CAPP_PACKAGE}_SUBDIRECTORY}" PARENT_SCOPE)
set(${CAPP_PACKAGE}_BUILD_TYPE "${${CAPP_PACKAGE}_BUILD_TYPE}" PARENT_SCOPE)
set(CAPP_PACKAGES ${CAPP_PACKAGES} ${CAPP_PACKAGE} PARENT_SCOPE)
endfunction()
macro(capp_find_root)
while (CAPP_TRUE)
get_filename_component(CAPP_ROOT_PARENT "${CAPP_ROOT}" DIRECTORY)
if (CAPP_ROOT_PARENT STREQUAL CAPP_ROOT)
message(FATAL_ERROR "CApp could not find app.cmake in ${CMAKE_CURRENT_SOURCE_DIR} or any parent directories: Run capp init first")
endif()
if (EXISTS "${CAPP_ROOT}/app.cmake")
break()
endif()
set(CAPP_ROOT "${CAPP_ROOT_PARENT}")
endwhile()
set(CAPP_SOURCE_ROOT "${CAPP_ROOT}/source")
set(CAPP_PACKAGE_ROOT "${CAPP_ROOT}/package")
capp_get_commit(
GIT_REPO_PATH "${CAPP_ROOT}"
COMMIT_VARIABLE CAPP_COMMIT
RESULT_VARIABLE get_commit_result)
if (NOT get_commit_result EQUAL 0)
message(FATAL_ERROR "CApp could not query the commit of the build repository")
endif()
endmacro()
macro(capp_setup_flavor)
cmake_parse_arguments(arg "OPTIONAL" "" "" ${ARGN})
if (NOT CAPP_FLAVOR)
set(flavor_dir_regex "${CAPP_ROOT}/flavor/([^/])")
if (CMAKE_CURRENT_SOURCE_DIR MATCHES "${flavor_dir_regex}")
string(REGEX REPLACE "${flavor_dir_regex}" "\\1" flavor "${CMAKE_CURRENT_SOURCE_DIR}")
set(CAPP_FLAVOR "${flavor}")
endif()
endif()
if (NOT CAPP_FLAVOR)
if (DEFINED ENV{CAPP_FLAVOR})
set(CAPP_FLAVOR $ENV{CAPP_FLAVOR})
endif()
endif()
if (CAPP_FLAVOR)
set(CAPP_FLAVOR_ROOT "${CAPP_ROOT}/flavor/${CAPP_FLAVOR}")
set(CAPP_BUILD_ROOT "${CAPP_FLAVOR_ROOT}/build")
if (NOT CAPP_PREFIX)
if (DEFINED ENV{CAPP_PREFIX})
set(CAPP_PREFIX $ENV{CAPP_PREFIX})
endif()
endif()
if (CAPP_PREFIX)
set(CAPP_INSTALL_ROOT "${CAPP_PREFIX}")
set(CAPP_VENV_ROOT "${CAPP_PREFIX}/venv")
else()
set(CAPP_INSTALL_ROOT "${CAPP_FLAVOR_ROOT}/install")
set(CAPP_VENV_ROOT "${CAPP_FLAVOR_ROOT}/venv")
endif()
set(flavor_file "${CAPP_FLAVOR_ROOT}/flavor.cmake")
if (NOT EXISTS "${flavor_file}")
message(FATAL_ERROR "CApp: Flavor file ${flavor_file} doesn't exist. It should define any CMake variables needed to setup this flavor of the overall build. If you have no such variables, creating an empty file will suffice.")
endif()
include("${flavor_file}")
else()
if (NOT arg_OPTIONAL)
capp_subdirectories(PARENT_DIRECTORY "${CAPP_ROOT}/flavor" RESULT_VARIABLE flavors)
string(REPLACE ";" ", " flavors "${flavors}")
message(FATAL_ERROR "CApp: No flavor has been selected. You can select a flavor by setting the CAPP_FLAVOR environment variable, giving the `-f <flavor>` command line flag to CApp, or by running CApp from inside a flavor subdirectory. Available flavors are: ${flavors}.")
endif()
endif()
endmacro()
macro(capp_recursive_read_package_file package)
list(FIND CAPP_PACKAGES ${package} list_index)
if (list_index EQUAL -1)
capp_read_package_file(PACKAGE ${package})
foreach(dependency IN LISTS ${package}_DEPENDENCIES)
capp_recursive_read_package_file(${dependency})
endforeach()
endif()
endmacro()
macro(capp_read_package_files_by_dependency)
include("${CAPP_ROOT}/app.cmake")
set(CAPP_PACKAGES)
foreach(root_package IN LISTS CAPP_ROOT_PACKAGES)
capp_recursive_read_package_file(${root_package})
endforeach()
endmacro()
macro(capp_read_all_package_files)
include("${CAPP_ROOT}/app.cmake")
set(CAPP_PACKAGES)
file(
GLOB packages
LIST_DIRECTORIES true
RELATIVE "${CAPP_PACKAGE_ROOT}"
"${CAPP_PACKAGE_ROOT}/*")
foreach(package IN LISTS packages)
capp_read_package_file(PACKAGE ${package})
endforeach()
endmacro()
function(capp_invalidate_config package)
if (${package}_NO_CONFIGURE_CACHE)
file(REMOVE "${CAPP_BUILD_ROOT}/${package}/CMakeCache.txt")
endif()
file(REMOVE "${CAPP_BUILD_ROOT}/${package}/capp_configured.txt")
endfunction()
function(capp_invalidate_install package)
file(REMOVE "${CAPP_BUILD_ROOT}/${package}/capp_installed.txt")
endfunction()
#CApp uses these "sentinel" files capp_installed.txt and capp_configured.txt to
#record whether a package has been configured or installed at a particular time.
#In order to see if one of these operations can be treated as a valid cached operation
#then we check whether these files exist and whether they are newer than the CMake
#files that affect the build like app.cmake and the configuration file.
function(capp_sentinel_file_valid)
cmake_parse_arguments(PARSE_ARGV 0 arg "" "PACKAGE;SENTINEL_FILE;RESULT_VARIABLE" "")
set(package "${arg_PACKAGE}")
set(sentinel_file "${arg_SENTINEL_FILE}")
set(result_variable "${arg_RESULT_VARIABLE}")
if (NOT EXISTS "${sentinel_file}")
set(${result_variable} FALSE PARENT_SCOPE)
return()
endif()
if (NOT "${sentinel_file}" IS_NEWER_THAN "${CAPP_FLAVOR_ROOT}/flavor.cmake")
set(${result_variable} FALSE PARENT_SCOPE)
return()
endif()
if (NOT "${sentinel_file}" IS_NEWER_THAN "${CAPP_ROOT}/app.cmake")
set(${result_variable} FALSE PARENT_SCOPE)
return()
endif()
if (NOT "${sentinel_file}" IS_NEWER_THAN "${CAPP_PACKAGE_ROOT}/${package}/package.cmake")
set(${result_variable} FALSE PARENT_SCOPE)
return()
endif()
set(${result_variable} TRUE PARENT_SCOPE)
endfunction()
function(capp_dependencies_installed)
cmake_parse_arguments(PARSE_ARGV 0 capp_dependencies_installed "" "PACKAGE;RESULT_VARIABLE" "")
set(dependencies_installed TRUE)
foreach (dependency IN LISTS ${capp_dependencies_installed_PACKAGE}_DEPENDENCIES)
if (NOT IS_DIRECTORY "${CAPP_PACKAGE_ROOT}/${dependency}")
message(FATAL_ERROR "${capp_dependencies_installed_PACKAGE} depends on ${dependency}, which is not a package")
endif()
if (NOT ${dependency}_IS_INSTALLED)
set(dependencies_installed FALSE)
endif()
endforeach()
set(${capp_dependencies_installed_RESULT_VARIABLE} ${dependencies_installed} PARENT_SCOPE)
endfunction()
function(capp_initialize_needs)
foreach(package IN LISTS CAPP_PACKAGES)
if (IS_DIRECTORY "${CAPP_SOURCE_ROOT}/${package}")
set(${package}_IS_CLONED TRUE)
else()
set(${package}_IS_CLONED FALSE)
endif()
endforeach()
foreach(package IN LISTS CAPP_PACKAGES)
set(${package}_IS_CONFIGURED TRUE)
if (NOT ${package}_IS_CLONED)
set(${package}_IS_CONFIGURED FALSE)
endif()
capp_sentinel_file_valid(
PACKAGE ${package}
SENTINEL_FILE "${CAPP_BUILD_ROOT}/${package}/capp_configured.txt"
RESULT_VARIABLE config_sentinel_valid)
if (NOT config_sentinel_valid)
set(${package}_IS_CONFIGURED FALSE)
endif()
if (NOT ${package}_IS_CONFIGURED)
capp_invalidate_config(${package})
endif()
endforeach()
foreach(package IN LISTS CAPP_PACKAGES)
set(${package}_IS_INSTALLED TRUE)
if (NOT ${package}_IS_CONFIGURED)
set(${package}_IS_INSTALLED FALSE)
endif()
capp_dependencies_installed(
PACKAGE ${package}
RESULT_VARIABLE dependencies_installed)
if (NOT dependencies_installed)
set(${package}_IS_INSTALLED FALSE)
endif()
capp_sentinel_file_valid(
PACKAGE ${package}
SENTINEL_FILE "${CAPP_BUILD_ROOT}/${package}/capp_installed.txt"
RESULT_VARIABLE install_sentinel_valid)
if (NOT install_sentinel_valid)
set(${package}_IS_INSTALLED FALSE)
endif()
if (NOT ${package}_IS_INSTALLED)
file(REMOVE "${CAPP_BUILD_ROOT}/${package}/capp_installed.txt")
endif()
endforeach()
foreach(package IN LISTS CAPP_PACKAGES)
set(${package}_IS_CLONED ${${package}_IS_CLONED} PARENT_SCOPE)
set(${package}_IS_CONFIGURED ${${package}_IS_CONFIGURED} PARENT_SCOPE)
set(${package}_IS_INSTALLED ${${package}_IS_INSTALLED} PARENT_SCOPE)
endforeach()
endfunction()
function(capp_fulfill_needs)
cmake_parse_arguments(PARSE_ARGV 0 capp_fulfill_needs "" "RESULT_VARIABLE" "BUILD_ARGUMENTS")
foreach(package IN LISTS CAPP_PACKAGES)
if (NOT ${package}_IS_CLONED)
capp_clone(
PACKAGE ${package}
RESULT_VARIABLE capp_clone_result
)
if (NOT capp_clone_result EQUAL 0)
message("capp_clone for ${package} failed")
set(${capp_fulfill_needs_RESULT_VARIABLE} "${capp_clone_result}" PARENT_SCOPE)
return()
endif()
set(${package}_IS_CLONED ${${package}_IS_CLONED} PARENT_SCOPE)
endif()
if (NOT ${package}_IS_CONFIGURED)
capp_configure(
PACKAGE ${package}
RESULT_VARIABLE capp_configure_result
)
if (NOT capp_configure_result EQUAL 0)
message("capp_configure for ${package} failed")
set(${capp_fulfill_needs_RESULT_VARIABLE} "${capp_configure_result}" PARENT_SCOPE)
return()