-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateCMakeFiles-lib.sh
executable file
·2256 lines (1903 loc) · 83.4 KB
/
GenerateCMakeFiles-lib.sh
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
#!/bin/bash
#
# Copyright (C) 2016-2022 Radek Malcic
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
GENERATE_CMAKE_VERSION="1.0"
GENERATE_DATE="$(export LC_ALL=C; date)"
OFN="CMakeLists.txt" # Output file name
LINK_LIST="LINK_LIST"
DEPEND_LIST="DEPEND_LIST"
SOURCE_LIST_C="SOURCE_LIST_C"
SOURCE_LIST_CPP="SOURCE_LIST_CPP"
HEADER_LIST="HEADER_LIST"
INCLUDE_LIST="INCLUDE_LIST"
SOURCE_LIST_ICPP="SOURCE_LIST_ICPP"
SOURCE_LIST_RC="SOURCE_LIST_RC"
COMPILE_FLAGS_LIST="COMPILE_FLAGS_LIST"
TARGET_RENAME="TARGET_RENAME"
PCH_FILE="PCH_FILE"
PCH_INCLUDE_LIST="PCH_INCLUDE_LIST"
PCH_COMPILE_DEFINITIONS="PCH_COMPILE_DEFINITIONS"
BIN_SUFFIX="-bin"
LIB_SUFFIX="-lib"
RE_BZIP2='[bB][zZ]2'
RE_ZIP='[zZ][iI][pP]'
RE_PNG='[pP][nN][gG]'
RE_C='\.([cC])$'
RE_CPP='\.([cC]+[xXpP]{0,2})$'
RE_ICPP='\.([iI][cC]+[xXpP]{0,2})$'
RE_RC='\.(rc)$'
RE_BRC='\.(brc)$'
RE_USES='^uses$'
RE_LINK='^link$'
RE_LIBRARY='^library$'
RE_PKG_CONFIG='^pkg_config$'
RE_STATIC_LIBRARY='^static_library$'
RE_OPTIONS='^options$'
RE_FILES='^file$'
RE_INCLUDE='^include$'
RE_TARGET='^target$'
RE_SEPARATOR='separator'
RE_IMPORT='import.ext'
RE_IMPORT_ADD='^files|^includes'
RE_IMPORT_DEL='^exclude'
RE_FILE_DOT='\.'
RE_FILE_SPLIT='(options|charset|optimize_speed|highlight)'
RE_FILE_EXCLUDE='(depends\(\))'
RE_FILE_PCH='(PCH)'
UPP_ALL_USES=()
UPP_ALL_USES_DONE=()
INCLUDE_SYSTEM_LIST=()
SECTIONS=("acceptflags" "charset" "custom" "description" "file" "flags" "include" "library" "static_library" "link" "optimize_size" "optimize_speed" "options" "mainconfig" "noblitz" "target" "uses" "pkg_config")
RE_SKIP_SECTIONS='(acceptflags|mainconfig|charset|description|optimize_size|optimize_speed|noblitz)'
get_section_name()
{
local line="${1}"
line="${line//\(/ }"
line="${line//\)/ }"
local tmp=(${line})
local name="$(string_trim_spaces_both "${tmp[0]}")"
if [[ " ${SECTIONS[@]} " =~ " ${name} " ]]; then
echo "${name}"
fi
}
get_section_line()
{
local section="${1}"
local line="$(string_trim_spaces_both "${2}")"
line="${line/#${section}/}"
echo "$(string_trim_spaces_both "${line}")"
}
test_required_binaries()
{
# Requirement for generating the CMakeList files
local my_sort="$(which sort)"
local my_date="$(which date)"
local my_find="$(which find)"
local my_xargs="$(which xargs)"
if [ -z "${my_sort}" ] || [ -z "${my_date}" ] || [ -z "${my_find}" ] || [ -z "${my_xargs}" ] ; then
echo "ERROR - Requirement for generating the CMakeList files failed."
echo "ERROR - Can not continue -> Exiting!"
echo "sort=\"${my_sort}\""
echo "date=\"${my_date}\""
echo "find=\"${my_find}\""
echo "xargs=\"${my_xargs}\""
exit 1
fi
}
string_trim_spaces_both()
{
local line="${1}"
line="${line#"${line%%[![:space:]]*}"}" # remove leading whitespace from a string
line="${line%"${line##*[![:space:]]}"}" # remove trailing whitespace from a string
echo "${line}"
}
string_remove_separators()
{
local line="${1}"
line="${line//,}" # Remove ','
line="${line//;}" # Remove ';'
echo "${line}"
}
string_remove_comma()
{
local line="$(string_remove_separators "${1}")"
line="${line//\"}" # Remove '"'
echo "${line}"
}
string_replace_dash()
{
local line="${1}"
line="${line//\//_}"
echo "${line}"
}
string_get_in_parenthesis()
{
local line="${1}"
if [[ "${line}" =~ \( ]]; then
# Get string inside parenthesis
line=${line#*(}
line=${line%)*}
line="${line//& }" # Remove all '& '
echo "${line}"
else
echo
fi
}
string_get_after_parenthesis()
{
local line="${1}"
line="${line##*) }" # Get string after the right parenthesis
echo "${line}"
}
string_get_before_parenthesis()
{
local line="${1}"
line="${line%%(*}" # Get string before the left parenthesis
echo "${line}"
}
if_options_replace()
{
local options="$(string_trim_spaces_both "${1}")"
local output=""
if [ -n "${options}" ]; then
case "${options}" in
"OR") # operand
output="OR"
;;
"SHARED")
output="BUILD_SHARED_LIBS"
;;
"WIN32")
output="WIN32"
;;
esac
if [ -n "${options}" ] && [ -z "${output}" ]; then
output="DEFINED flag${options}"
fi
echo "${output}"
fi
}
if_options_parse()
{
local operand=""
local next_operand=" AND "
local counter=0
local output=""
local list=""
local options_replacement="${1/|/ | }"
local OPTIONS=(${options_replacement})
if [ -n "${OPTIONS}" ]; then
for list in "${OPTIONS[@]}"; do
# Don't process alone '!' operand
if [[ "${list}" =~ '!' ]] && [ "${#list}" -eq 1 ]; then
list=""
fi
if [ -n "${list}" ]; then
(( counter++ ))
operand="${next_operand}"
if [ "${list}" = '|' ]; then
operand=" "
list="OR"
next_operand=" "
else
next_operand=" AND "
fi
if [[ "${list}" =~ '!' ]]; then
list="${list//!}"
if [ "${counter}" -eq 1 ]; then
operand="NOT "
else
operand+="NOT "
fi
fi
# Don't insert 'AND operand as first option parameter
if [ "${counter}" -eq 1 ] && [[ "${operand}" = " AND " ]]; then
operand=""
fi
list="$(if_options_replace "${list}")"
output+="${operand}${list}"
fi
done
echo "${output}"
fi
}
if_options_parse_all()
{
local line="${1}"
local ALL_OPTIONS=()
local list=""
local output=""
local result=""
# Split options
local begin=0
local brace=0
for i in $( seq 0 $(( ${#line} )) ); do
if [ "${line:${i}:1}" == "(" ]; then
local length=$((i - begin))
if [ ${length} -gt 1 ]; then
ALL_OPTIONS+=("${line:${begin}:${length}}")
fi
begin=$((i + 1))
(( brace++ ))
fi
if [ ${brace} -gt 0 ] && [ "${line:${i}:1}" == ")" ]; then
local length=$((i - begin))
if [ ${length} -gt 1 ]; then
ALL_OPTIONS+=("${line:${begin}:${length}}")
fi
begin=$((i + 1))
(( brace-- ))
fi
done
if [ $begin -lt ${#line} ]; then
ALL_OPTIONS+=("${line:${begin}}")
fi
if [ ${#ALL_OPTIONS[@]} -eq 0 ]; then
ALL_OPTIONS+=("$(string_trim_spaces_both "${line}")")
fi
# Process options
if [ -n "${ALL_OPTIONS}" ]; then
for list in "${ALL_OPTIONS[@]}"; do
result="("$(if_options_parse "${list}")")" # Parse options
result="${result//\(OR / OR \(}" # Move 'OR'
result="${result//\(\)}" # Delete empty parenthesis
output+="${result}"
done
fi
echo "${output//\)\(/\) AND \(}" # Put 'AND' between options
}
add_require_for_lib()
{
local link_list="${1}"
local check_lib_name="${2}"
local pkg_config_module="${3}"
local req_lib_dir="DIRS"
local req_lib_name=""
local req_lib_param=""
local use_pkg="0"
if [ "${pkg_config_module}" == "1" ]; then
req_lib_name="${check_lib_name}"
req_lib_param="${check_lib_name}"
use_pkg="1"
fi
if [ -n "${req_lib_name}" ]; then
if [ "${use_pkg}" == "0" ]; then
echo " find_package ( ${req_lib_name} REQUIRED ${req_lib_param} )" >> "${OFN}"
else
echo " find_package ( PkgConfig REQUIRED )" >> "${OFN}"
echo " pkg_check_modules ( ${req_lib_name^^} REQUIRED ${req_lib_param})" >> "${OFN}"
fi
echo " if ( ${req_lib_name^^}_FOUND )" >> "${OFN}"
echo " list ( APPEND ${INCLUDE_LIST} \${${req_lib_name^^}_INCLUDE_${req_lib_dir}} )" >> "${OFN}"
echo " list ( APPEND ${link_list} \${${req_lib_name^^}_LIBRARIES} )" >> "${OFN}"
echo " # remove leading or trailing whitespace (e.g. for SDL2)" >> "${OFN}"
echo " if ( ${link_list} )" >> "${OFN}"
echo " string ( STRIP \"\${${link_list}}\" ${link_list} )" >> "${OFN}"
echo " endif()" >> "${OFN}"
if [ "${check_lib_name}" == "pthread" ]; then
echo " if ( CMAKE_THREAD_LIBS_INIT )" >> "${OFN}"
echo " list ( APPEND ${link_list} \${CMAKE_THREAD_LIBS_INIT} )" >> "${OFN}"
echo " endif()" >> "${OFN}"
fi
echo " endif()" >> "${OFN}"
else
echo "${check_lib_name}"
fi
}
add_all_uses() {
local value="$1"
if [[ ! " ${UPP_ALL_USES[@]} " =~ " ${value} " ]]; then
UPP_ALL_USES+=(${value})
fi
}
list_parse()
{
local line="${1}"
local list="${2}"
local target_name="${3}"
local list_append="${4}"
local options=""
local parameters=""
echo >> "${OFN}"
if [ -z "${list_append}" ]; then
echo "# ${line}" >> "${OFN}"
else
echo "# ${list_append} ${line}" >> "${OFN}"
fi
# echo "\"line: $line\""
if [[ "${line}" =~ BUILDER_OPTION ]]; then
$(if_options_builder "${line}")
else
if [ -z "${list_append}" ]; then
options="$(string_get_in_parenthesis "${line}")"
# echo "\"options: $options\""
options=$(if_options_parse_all "${options}") # Parse options
# echo "\"options: $options\""
parameters="$(string_get_after_parenthesis "${line}")"
parameters="$(string_remove_comma "${parameters}")"
# echo "\"param : $parameters\""
else
# echo "\"options:\""
parameters="$(string_remove_comma "${line}")"
# echo "\"param : $parameters\""
fi
# echo "\"list : $list\""
if [ -n "${options}" ] ; then
echo "if (${options})" >> "${OFN}"
fi
# Add optional dependency target to generate CMakeLists.txt
if [[ "${list}" =~ "${DEPEND_LIST}" ]]; then
local -a new_parameters=("${parameters}")
parameters=""
for item in ${new_parameters[@]}; do
parameters+="$(string_replace_dash "${item}${LIB_SUFFIX}") "
add_all_uses "${item}"
done
local trim_link_parameters="$(string_trim_spaces_both "${parameters}")"
if [ -n "${trim_link_parameters}" ]; then
echo " list ( APPEND ${list} ${trim_link_parameters} )" >> "${OFN}"
fi
fi
local add_link_library=""
if [ -n "${target_name}" ]; then
local pkg_config_module="0"
if [[ "${line}" =~ ^pkg_config || "${list_append}" =~ ^pkg_config ]]; then
pkg_config_module="1"
fi
local -a check_library_array=(${parameters})
for check_library in "${check_library_array[@]}"; do
add_link_library+="$(add_require_for_lib "${list}" "${check_library}" "${pkg_config_module}") "
done
fi
local trim_link_library="$(string_trim_spaces_both "${add_link_library}")"
if [ -n "${trim_link_library}" ]; then
echo " list ( APPEND ${list} ${trim_link_library} )" >> "${OFN}"
fi
if [ -n "${options}" ] ; then
echo "endif()" >> "${OFN}"
fi
fi
}
target_parse()
{
local line="${1}"
local options=""
local parameters=""
echo >> "${OFN}"
echo "#${1}" >> "${OFN}"
line="${line/#${section}/}"
options="$(string_get_in_parenthesis "${line}")"
if [ -n "${options}" ]; then
options="$(if_options_parse_all "${options}")" # Parse options
fi
parameters="$(string_get_after_parenthesis "${line}")"
parameters="${parameters//;}"
parameters="${parameters//\"}"
parameters="$(string_trim_spaces_both "${parameters}")"
if [ -n "${options}" ]; then
echo "if (${options})" >> "${OFN}"
echo " set ( ${TARGET_RENAME} \"${parameters}\" PARENT_SCOPE )" >> "${OFN}"
echo "endif()" >> "${OFN}"
else
echo "set ( ${TARGET_RENAME} \"${parameters}\" PARENT_SCOPE )" >> "${OFN}"
fi
}
link_parse()
{
local line="${1}"
local options=""
local parameters=""
echo >> "${OFN}"
echo "# ${1}" >> "${OFN}"
options="$(string_get_in_parenthesis "${line}")"
if [ -n "${options}" ]; then
options="$(if_options_parse_all "${options}")" # Parse options
fi
parameters="$(string_get_after_parenthesis "${line}")"
parameters="${parameters//;}"
parameters="${parameters//\"}"
if [ -n "${options}" ]; then
echo "if (${options})" >> "${OFN}"
echo " set ( MAIN_TARGET_LINK_FLAGS "\${MAIN_TARGET_LINK_FLAGS} ${parameters}" PARENT_SCOPE )" >> "${OFN}"
echo "endif()" >> "${OFN}"
fi
}
if_options_builder()
{
local line="${1}"
local options="$(string_get_after_parenthesis "${line}")"
local parameters_gcc=""
local parameters_msvc=""
if [[ "${options}" =~ NOWARNINGS ]]; then
parameters_gcc="-w"
parameters_msvc="-W0"
fi
if [ -n "${parameters_gcc}" ]; then
echo 'if ( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG )' >> "${OFN}"
echo " set ( CMAKE_CXX_FLAGS_\${CMAKE_BUILD_TYPE} \"\${CMAKE_CXX_FLAGS_\${CMAKE_BUILD_TYPE}} ${parameters_gcc}\")" >> "${OFN}"
echo " set ( CMAKE_C_FLAGS_\${CMAKE_BUILD_TYPE} \"\${CMAKE_C_FLAGS_\${CMAKE_BUILD_TYPE}} ${parameters_gcc}\")" >> "${OFN}"
echo 'elseif ( MSVC )' >> "${OFN}"
echo " set ( CMAKE_CXX_FLAGS_\${CMAKE_BUILD_TYPE} \"\${CMAKE_CXX_FLAGS_\${CMAKE_BUILD_TYPE}} ${parameters_msvc}\")" >> "${OFN}"
echo " set ( CMAKE_C_FLAGS_\${CMAKE_BUILD_TYPE} \"\${CMAKE_C_FLAGS_\${CMAKE_BUILD_TYPE}} ${parameters_msvc}\")" >> "${OFN}"
echo 'endif()' >> "${OFN}"
fi
}
binary_resource_parse()
{
local parse_file="${1}"
local binary_array_first_def=""
local binary_mask_first_def=""
if [ -n "${parse_file}" ] && [ -f "${parse_file}" ]; then
local line=""
local -a lines
local -a binary_array_names
local -a binary_array_names_library
mapfile -t lines < "${parse_file}"
for line in "${lines[@]}"; do
# Remove DOS line ending
line="${line//[$'\r']/}"
if [ -n "${line}" ]; then
local parameter="$(string_get_before_parenthesis "${line}")"
parameter="$(string_trim_spaces_both "${parameter}")"
local options="$(string_get_in_parenthesis "${line}")"
read -d '' -ra options_params < <(printf '%s\0' "${options}")
if [ "${parameter}" == "BINARY_ARRAY" ]; then
local symbol_name="$(string_trim_spaces_both "${options_params[0]//,}")"
local symbol_name_array="$(string_trim_spaces_both "${options_params[1]//,}")"
local symbol_file_name="$(string_trim_spaces_both "${options_params[2]//\"}")"
local symbol_file_compress="${options_params[4]}"
else
local symbol_name="$(string_trim_spaces_both "${options_params[0]//,}")"
local symbol_file_name="$(string_trim_spaces_both "${options_params[1]//\"}")"
local symbol_file_compress="${options_params[2]}"
fi
if [ -z "${symbol_file_compress}" ]; then
symbol_file_compress="none"
fi
# Parse BINARY resources
if [ "${parameter}" == "BINARY" ]; then
echo >> "${OFN}"
echo "# BINARY file" >> "${OFN}"
echo "create_brc_source ( ${symbol_file_name} ${symbol_name}.cpp ${symbol_name} ${symbol_file_compress} write )" >> "${OFN}"
echo "set_source_files_properties ( \${CMAKE_CURRENT_BINARY_DIR}/${symbol_name}.cpp PROPERTIES GENERATED TRUE )" >> "${OFN}"
echo "list ( APPEND ${SOURCE_LIST_CPP} \${CMAKE_CURRENT_BINARY_DIR}/${symbol_name}.cpp )" >> "${OFN}"
# parse BINARY_ARRAY resources
elif [ "${parameter}" == "BINARY_ARRAY" ]; then
local file_creation="append"
if [ -z "${binary_array_first_def}" ]; then
binary_array_first_def="done"
file_creation="write"
fi
binary_array_names+=("${symbol_name}_${symbol_name_array}")
echo >> "${OFN}"
echo "# BINARY_ARRAY file" >> "${OFN}"
echo "create_brc_source ( ${symbol_file_name} binary_array.cpp ${symbol_name}_${symbol_name_array} ${symbol_file_compress} ${file_creation} )" >> "${OFN}"
# parse BINARY_MASK resources
elif [ "${parameter}" == "BINARY_MASK" ]; then
local -a binary_mask_files=("$(eval echo "${symbol_file_name}")")
if [ -n "${binary_mask_files}" ]; then
local all_count=0
local binary_file=""
local -a all_array_files
for binary_file in "${binary_mask_files[@]}"; do
if [ -f "${binary_file}" ]; then
local file_creation="append"
if [ -z "${binary_mask_first_def}" ]; then
binary_mask_first_def="done"
file_creation="write"
fi
echo >> "${OFN}"
echo "# BINARY_MASK file" >> "${OFN}"
echo "create_brc_source ( ${binary_file} ${symbol_name}.cpp ${symbol_name}_${all_count} ${symbol_file_compress} ${file_creation} )" >> "${OFN}"
all_array_files+=("$(basename "${binary_file}")")
(( all_count++ ))
fi
done
# Generate cpp file for the BINARY_MASK
echo >> "${OFN}"
echo "# Append additional information of the BINARY_MASK binary resource (${symbol_name})" >> "${OFN}"
echo "file ( APPEND \${CMAKE_CURRENT_BINARY_DIR}/${symbol_name}.cpp \"" >> "${OFN}"
echo "int ${symbol_name}_count = ${all_count};" >> "${OFN}"
echo "int ${symbol_name}_length[] = {" >> "${OFN}"
for (( i=0; i<${all_count}; i++ )); do
echo " ${symbol_name}_${i}_length," >> "${OFN}"
done
echo "};" >> "${OFN}"
echo "unsigned char *${symbol_name}[] = {" >> "${OFN}"
for (( i=0; i<${all_count}; i++ )); do
echo " ${symbol_name}_${i}_," >> "${OFN}"
done
echo "};" >> "${OFN}"
echo "char const *${symbol_name}_files[] = {" >> "${OFN}"
local binary_filename=""
for binary_file_name in "${all_array_files[@]}"; do
echo " \\\"${binary_file_name}\\\"," >> "${OFN}"
done
echo "};" >> "${OFN}"
echo "\")" >> "${OFN}"
echo "set_source_files_properties ( \${CMAKE_CURRENT_BINARY_DIR}/${symbol_name}.cpp PROPERTIES GENERATED TRUE )" >> "${OFN}"
echo "list ( APPEND ${SOURCE_LIST_CPP} \${CMAKE_CURRENT_BINARY_DIR}/${symbol_name}.cpp )" >> "${OFN}"
else
echo >> "${OFN}"
echo "# BINARY_MASK file" >> "${OFN}"
echo "# No files match the mask: '${symbol_file_name}'" >> "${OFN}"
fi
fi # BINARY end
fi
done
# Generate cpp file for the BINARY_ARRAY
if [ -n "${binary_array_names}" ]; then
# echo "# ${binary_array_names[@]}" >> "${OFN}"
local test_first_iteration
local binary_array_name_count=0
local binary_array_name_test
local binary_array_name_first
local binary_array_name_second
echo >> "${OFN}"
echo "# Append additional information of the BINARY_ARRAY binary resource" >> "${OFN}"
echo "file ( APPEND \${CMAKE_CURRENT_BINARY_DIR}/binary_array.cpp \"" >> "${OFN}"
for binary_array_record in "${binary_array_names[@]}"; do
binary_array_name_split=(${binary_array_record//_[0-9]/ })
if [ ! "${binary_array_name_split[0]}" == "${binary_array_name_test}" ]; then
if [ -z "${test_first_iteration}" ]; then
test_first_iteration="done"
else
echo "int ${binary_array_name_test}_count = ${binary_array_name_count};" >> "${OFN}"
echo -e "${binary_array_name_first}" >> "${OFN}"
echo -e "};\n" >> "${OFN}"
echo -e "${binary_array_name_second}" >> "${OFN}"
echo -e "};\n" >> "${OFN}"
binary_array_name_count=0
fi
binary_array_name_test=${binary_array_name_split[0]};
binary_array_name_first="int ${binary_array_name_split[0]}_length[] = {"
binary_array_name_second="unsigned char *${binary_array_name_split[0]}[] = {"
fi
(( binary_array_name_count++ ))
binary_array_name_first+="\n ${binary_array_record}_length,"
binary_array_name_second+="\n ${binary_array_record}_,"
done
echo "int ${binary_array_name_test}_count = ${binary_array_name_count};" >> "${OFN}"
echo -e "${binary_array_name_first}" >> "${OFN}"
echo -e "};" >> "${OFN}"
echo -e "${binary_array_name_second}" >> "${OFN}"
echo -e "};" >> "${OFN}"
echo "\")" >> "${OFN}"
echo "set_source_files_properties ( \${CMAKE_CURRENT_BINARY_DIR}/binary_array.cpp PROPERTIES GENERATED TRUE )" >> "${OFN}"
echo "list ( APPEND ${SOURCE_LIST_CPP} \${CMAKE_CURRENT_BINARY_DIR}/binary_array.cpp )" >> "${OFN}"
fi
else
echo "File \"${parse_file}\" not found!"
fi
}
import_ext_parse()
{
local parse_file="$(string_remove_comma ${1})"
local files_add=0
local files_del=0
local line=""
local -a lines
local -a added_files
local -a excluded_files
local -a result
mapfile -t lines < "${parse_file}"
for line in "${lines[@]}"; do
# Remove DOS line ending
line="${line//[$'\r']/}"
# Begin of the add section
if [[ "${line}" =~ $RE_IMPORT_ADD ]]; then
files_add=1
fi
# Begin of the del section
if [[ "${line}" =~ $RE_IMPORT_DEl ]]; then
files_del=1
fi
if [ "${files_add}" -gt 0 ]; then
# End of the add section (line with ';')
if [[ ${line} =~ ';' ]]; then
files_add=2
fi
# Remove ',' and ';'
line="$(string_remove_separators "${line}")"
# Convert line to array
read -a line_array <<< "${line}"
for list in "${line_array[@]}"; do
list="$(string_remove_separators "${list}")"
if [[ ! "${list}" =~ $RE_IMPORT_ADD ]]; then
if [[ "${list}" =~ "*" ]]; then
added_files+=("$(find -name "${list}")")
else
added_files+=("$(find -nowarn -samefile "${list}" 2>/dev/null)")
fi
fi
done
if [ "${files_add}" -eq 2 ]; then
files_add=-1
fi
fi
if [ "${files_del}" -gt 0 ]; then
# End of the del section (line with ';')
if [ "${files_del}" -gt 0 ] && [[ "${line}" =~ ';' ]]; then
files_del=2
fi
# Remove ',' and ';'
line="$(string_remove_separators "${line}")"
# Convert line to array
read -a line_array <<< "${line}"
for list in "${line_array[@]}"; do
list="$(string_remove_separators "${list}")"
if [[ ! "${list}" =~ $RE_IMPORT_DEl ]]; then
if [[ "${list}" =~ "*" ]]; then
excluded_files+=("$(find -name "${list}")")
else
excluded_files+=("$(find -samefile "${list}" 2>/dev/null)")
fi
fi
done
if [ "${files_del}" -eq 2 ]; then
files_del=-1
fi
fi
done
for value in "${added_files[@]}"; do
if [[ ! " ${excluded_files[@]} " =~ " ${value} " ]]; then
result+=(${value})
fi
done
echo "${result[@]}"
}
generate_cmake_header()
{
if [ -f ""${OFN}"" ]; then
rm ""${OFN}""
fi
cat > "${OFN}" << EOL
# ${OFN} generated ${GENERATE_DATE}
cmake_minimum_required ( VERSION 3.4.1 )
#################################################
# In-Source builds are strictly prohibited.
#################################################
if ( \${CMAKE_SOURCE_DIR} STREQUAL \${CMAKE_BINARY_DIR} )
message ( FATAL_ERROR
"\n****************************** ERROR ******************************\n"
"In-source build are not allowed. "
"Please do not polute the sources with binaries or any project unrelated files. "
"To remove generated files run:\n"
"'rm -rf CMakeCache.txt CMakeFiles'\n"
"To build the project, please do the following:\n"
"'mkdir build && cd build && cmake ..'"
"\n****************************** ERROR ******************************\n")
endif()
# Set the default library directory to store built libraries
set ( LIBRARY_OUTPUT_PATH \${PROJECT_BINARY_DIR}/lib )
EOL
}
generate_cmake_from_upp()
{
local upp_ext="${1}"
local object_name="${2}"
local main_target="${3}"
local USES=()
local HEADER=()
local SOURCE_C=()
local SOURCE_CPP=()
local SOURCE_RC=()
local SOURCE_ICPP=()
local OPTIONS=()
local tmp=""
local line=""
local dir_array=()
INCLUDE_SYSTEM_LIST=()
if [ -f "${upp_ext}" ]; then
local target_name="$(string_replace_dash "${object_name}")"
local name=""
local content=()
local section_name=()
local section_content=()
# parse upp file
while read -r line; do
# Replace '\' to '/'
line="${line//\\//}"
# Remove DOS line ending
line="${line//[$'\r']/}"
test_name="$(get_section_name "${line}")"
if [ ! "${test_name}" == "" ]; then
if [ ! "${name}" == "" ]; then
section_name+=("${name}")
section_content+=("$(printf " \'%s\' " "${content[@]}")")
content=()
fi
name="${test_name}"
fi
section_line="$(get_section_line "${name}" "${line}")"
if [ "${section_line}" == "" ]; then
continue;
fi
content+=("${section_line}")
done < "${upp_ext}"
section_name+=("${name}")
section_content+=("$(printf " \'%s\' " "${content[@]}")")
# process sections
for index in ${!section_name[@]}; do
local section="${section_name[$index]}"
if [[ "${section}" =~ $RE_SKIP_SECTIONS ]]; then
continue;
fi
content=()
while read word; do
content+=("$word")
done < <(echo "${section_content[$index]}" | xargs -n 1)
# echo "section: ${section} (${#content[@]})"
# echo "content: ${content[@]}"
# echo "data : ${section_content[$index]}"
# echo "===================================================================="
# Parse target options
if [ -n "${main_target}" ] && [[ "${section}" =~ $RE_TARGET ]]; then
for LINE in "${content[@]}"; do
target_parse "target ${LINE}"
done
fi
# Parse compiler options
if [[ "${section}" =~ $RE_USES ]]; then
for LINE in "${content[@]}"; do
if [[ "${LINE:0:1}" == "(" ]] && [[ ${LINE} =~ ';' ]]; then
list_parse "uses${LINE}" ${target_name}_${DEPEND_LIST}
else
tmp="$(string_remove_separators "${LINE}")"
USES+=(${tmp})
add_all_uses "${tmp}"
fi
done
fi
# Parse library list options
if [[ "${section}" =~ $RE_LIBRARY ]] || [[ "${section}" =~ $RE_PKG_CONFIG ]] || [[ "${section}" =~ $RE_STATIC_LIBRARY ]]; then
for LINE in "${content[@]}"; do
if [[ "${LINE:0:1}" == "(" ]] && [[ "${LINE}" =~ ';' ]]; then
if [[ "${section}" =~ $RE_PKG_CONFIG ]]; then
list_parse "pkg_config${LINE}" "${LINK_LIST}" "${target_name}"
else
list_parse "library${LINE}" "${LINK_LIST}" "${target_name}"
fi
else
if [[ "${section}" =~ $RE_PKG_CONFIG ]]; then
list_parse "${LINE}" "${LINK_LIST}" "${target_name}" "pkg_config"
else
list_parse "${LINE}" "${LINK_LIST}" "${target_name}" "append library"
fi
fi
done
fi
# Parse options section
if [[ "${section}" =~ $RE_OPTIONS ]]; then
for LINE in "${content[@]}"; do
if [[ "${LINE:0:1}" == "(" ]] && [[ "${LINE}" =~ ';' ]]; then
list_parse "options${LINE}" "${COMPILE_FLAGS_LIST}" "${target_name}"
else
tmp="$(string_remove_separators "${LINE}")"
OPTIONS+=(${tmp})
fi
done
fi
# Parse include options
if [[ "${section}" =~ $RE_INCLUDE ]]; then
for LINE in "${content[@]}"; do
LINE="$(string_remove_separators "${LINE}")"
INCLUDE_SYSTEM_LIST+=("${LINE}")
done
fi
# Parse link options
if [[ "${section}" =~ $RE_LINK ]]; then
for LINE in "${content[@]}"; do
link_parse "link${LINE}"
done
fi
# Parse files
if [[ "${section}" =~ $RE_FILES ]]; then
local list=""
local line_array=()
for LINE in "${content[@]}"; do
# Skip lines with "separator" mark
if [[ "${LINE}" =~ $RE_SEPARATOR ]]; then
continue
fi
# Find precompiled header option
if [[ "${LINE}" =~ $RE_FILE_PCH ]] && [[ "${LINE}" =~ BUILDER_OPTION ]]; then
local pch_file=${LINE// */}
echo >> "${OFN}"
echo '# Precompiled headers file' >> "${OFN}"
echo "set ( ${PCH_FILE} "\${CMAKE_CURRENT_SOURCE_DIR}/${pch_file}" )" >> "${OFN}"
fi
# Split lines with charset, options, ...
if [[ "${LINE}" =~ $RE_FILE_SPLIT ]]; then
LINE="${LINE// */}"
fi
if [[ "${LINE}" =~ $RE_IMPORT ]]; then
line_array=("$(import_ext_parse "${LINE}")")
dir_array=("$(dirname ${line_array[@]} | sort -u)")
else
line_array+=(${LINE})
fi
done
for list in "${line_array[@]}"; do