summaryrefslogtreecommitdiff
path: root/xmake/modules/package/tools/xmake.lua
blob: 6f7f6e8846286f6b2688c73bac56904842be9d0c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        xmake.lua
--

-- imports
import("core.base.option")
import("core.base.global")
import("core.tool.toolchain")
import("core.project.project")
import("core.package.repository")
import("private.action.require.impl.package", {alias = "require_package"})
import("private.utils.toolchain", {alias = "toolchain_utils"})

-- get config from toolchains
function _get_config_from_toolchains(package, name)
    for _, toolchain_inst in ipairs(package:toolchains()) do
        local value = toolchain_inst:config(name)
        if value ~= nil then
            return value
        end
    end
end

-- is the toolchain compatible with the host?
function _is_toolchain_compatible_with_host(package)
    for _, name in ipairs(package:config("toolchains")) do
        if toolchain_utils.is_compatible_with_host(name) then
            return true
        end
    end
end

-- get configs for qt
function _get_configs_for_qt(package, configs, opt)
    local names = {"qt", "qt_sdkver"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
end

-- get configs for vcpkg
function _get_configs_for_vcpkg(package, configs, opt)
    local names = {"vcpkg"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
end

-- get configs for windows
function _get_configs_for_windows(package, configs, opt)
    local names = {"vs", "vs_toolset"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    -- pass runtimes from package configs
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)

    -- we can switch some toolchains, e.g. llvm/clang
    if package:config("toolchains") and _is_toolchain_compatible_with_host(package) then
        _get_configs_for_host_toolchain(package, configs, opt)
    end

    if not is_host("windows") then
        local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk")
        if sdkdir and #sdkdir > 0 then
            table.insert(configs, "--sdk=" .. sdkdir)
        end
    end
end

-- get configs for appleos
function _get_configs_for_appleos(package, configs, opt)
    local xcode = get_config("xcode")
    if xcode then
        table.insert(configs, "--xcode=" .. xcode)
    end
    local xcode_sdkver = get_config("xcode_sdkver")
    if xcode_sdkver then
        table.insert(configs, "--xcode_sdkver=" .. xcode_sdkver)
    end
    local target_minver = get_config("target_minver")
    if target_minver then
        table.insert(configs, "--target_minver=" .. target_minver)
    end
    local appledev = get_config("appledev")
    if appledev then
        table.insert(configs, "--appledev=" .. appledev)
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for android
function _get_configs_for_android(package, configs, opt)
    local names = {"ndk", "ndk_sdkver", "ndk_stdcxx", "ndk_cxxstl"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for mingw
function _get_configs_for_mingw(package, configs, opt)
    local names = {"mingw", "sdk", "ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for generic, e.g. linux, macosx, bsd host platforms
function _get_configs_for_generic(package, configs, opt)
    local names = {"ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
    if package:is_plat("macosx") then
        table.join2(names, "xcode", "xcode_sdkver", "target_minver", "appledev")
    end
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for host toolchain
function _get_configs_for_host_toolchain(package, configs, opt)
    local bindir = _get_config_from_toolchains(package, "bindir") or get_config("bin")
    if bindir then
        table.insert(configs, "--bin=" .. bindir)
    end
    local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk")
    if sdkdir then
        table.insert(configs, "--sdk=" .. sdkdir)
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    local toolchain_name = get_config("toolchain")
    if toolchain_name then
        table.insert(configs, "--toolchain=" .. toolchain_name)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for cross
function _get_configs_for_cross(package, configs, opt)
    local cross = _get_config_from_toolchains(package, "cross") or get_config("cross")
    if cross then
        table.insert(configs, "--cross=" .. cross)
    end
    local bindir = _get_config_from_toolchains(package, "bindir") or get_config("bin")
    if bindir then
        table.insert(configs, "--bin=" .. bindir)
    end
    local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk")
    if sdkdir then
        table.insert(configs, "--sdk=" .. sdkdir)
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    local toolchain_name = get_config("toolchain")
    if toolchain_name then
        table.insert(configs, "--toolchain=" .. toolchain_name)
    end
    local names = {"ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
end

-- get configs
function _get_configs(package, configs, opt)
    opt = opt or {}
    local configs  = configs or {}
    local cflags   = table.join(table.wrap(package:config("cflags")),   get_config("cflags"))
    local cxflags  = table.join(table.wrap(package:config("cxflags")),  get_config("cxflags"))
    local cxxflags = table.join(table.wrap(package:config("cxxflags")), get_config("cxxflags"))
    local asflags  = table.join(table.wrap(package:config("asflags")),  get_config("asflags"))
    local ldflags  = table.join(table.wrap(package:config("ldflags")),  get_config("ldflags"))
    local shflags  = table.join(table.wrap(package:config("shflags")),  get_config("shflags"))
    table.insert(configs, "--plat=" .. package:plat())
    table.insert(configs, "--arch=" .. package:arch())
    if configs.mode == nil then
        table.insert(configs, "--mode=" .. (package:is_debug() and "debug" or "release"))
    end
    if configs.kind == nil then
        table.insert(configs, "--kind=" .. (package:config("shared") and "shared" or "static"))
    end
    if package:is_plat("windows") then
        _get_configs_for_windows(package, configs, opt)
    elseif package:is_plat("android") then
        _get_configs_for_android(package, configs, opt)
    elseif package:is_plat("iphoneos", "watchos", "appletvos") or
        -- for cross-compilation on macOS, @see https://github.com/xmake-io/xmake/issues/2804
        (package:is_plat("macosx") and (get_config("appledev") or not package:is_arch(os.subarch()))) then
        _get_configs_for_appleos(package, configs, opt)
    elseif package:is_plat("mingw") then
        _get_configs_for_mingw(package, configs, opt)
    elseif package:is_cross() then
        _get_configs_for_cross(package, configs, opt)
    elseif package:config("toolchains") then
        -- we still need find system libraries,
        -- it just pass toolchain environments if the toolchain is compatible with host
        if _is_toolchain_compatible_with_host(package) then
            _get_configs_for_host_toolchain(package, configs, opt)
        else
            _get_configs_for_cross(package, configs, opt)
        end
    else
        _get_configs_for_generic(package, configs, opt)
    end

    local policies = get_config("policies")
    local policies_list = policies and policies:split(",") or {}
    if package:config("lto") and (not policies or not policies:find("build.optimization.lto", 1, true)) then
        table.insert(policies_list, "build.optimization.lto")
    end
    if package:config("asan") and (not policies or not policies:find("build.sanitizer.address", 1, true)) then
        table.insert(policies_list, "build.sanitizer.address")
    end
    if not package:use_external_includes() and (not policies or not policies:find("package.include_external_headers", 1, true)) then
        table.insert(policies_list, "package.include_external_headers:n")
    end
    if policies:find("package.build.ccache", 1, true) then
        table.insert(configs, "--ccachedir=" .. path.join(path.directory(package:cachedir()), "build_cache"))
        table.insert(policies_list, "build.ccache")
    end
    if policies then
        table.insert(configs, "--policies=" .. table.concat(policies_list, ","))
    end

    if not package:is_plat("windows", "mingw") and package:config("pic") ~= false then
        table.insert(cxflags, "-fPIC")
    end
    if cflags and #cflags > 0 then
        table.insert(configs, "--cflags=" .. table.concat(cflags, ' '))
    end
    if cxflags and #cxflags > 0 then
        table.insert(configs, "--cxflags=" .. table.concat(cxflags, ' '))
    end
    if cxxflags and #cxxflags > 0 then
        table.insert(configs, "--cxxflags=" .. table.concat(cxxflags, ' '))
    end
    if asflags and #asflags > 0 then
        table.insert(configs, "--asflags=" .. table.concat(asflags, ' '))
    end
    if ldflags and #ldflags > 0 then
        table.insert(configs, "--ldflags=" .. table.concat(ldflags, ' '))
    end
    if shflags and #shflags > 0 then
        table.insert(configs, "--shflags=" .. table.concat(shflags, ' '))
    end
    local builddir = opt.builddir or opt.buildir or package:builddir()
    if builddir then
        table.insert(configs, "--builddir=" .. builddir)
    end
    if opt.buildir then
        wprint("{buildir = } has been deprecated, please use {builddir = } in xmake.install")
    end
    return configs
end

-- maybe in project?
-- @see https://github.com/xmake-io/xmake/issues/3720
function _maybe_in_project(package)
    local dir = package:sourcedir() or package:cachedir()
    local parentdir = path.directory(dir)
    while parentdir and os.isdir(parentdir) do
        if os.isfile(path.join(parentdir, "xmake.lua")) then
            return true
        end
        parentdir = path.directory(parentdir)
    end
end

-- set some builtin global options from the parent xmake
function _set_builtin_argv(package, argv)
    -- if the package cache directory is modified,
    -- we need to force the project directory to be specified to avoid interference by the upper level xmake.lua.
    -- and we also need to put `-P` in the first argument to avoid option.parse() parsing errors
    if _maybe_in_project(package) then
        table.insert(argv, "-P")
        table.insert(argv, os.curdir())
    end
    for _, name in ipairs({"diagnosis", "verbose", "quiet", "yes", "confirm", "root"}) do
        local value = option.get(name)
        if type(value) == "boolean" then
            table.insert(argv, "--" .. name)
        elseif value ~= nil then
            table.insert(argv, "--" .. name .. "=" .. value)
        end
    end
end

-- get require info of package
function _get_package_requireinfo(packagename)
    if os.isfile(os.projectfile()) then
        local requires_str, requires_extra = project.requires_str()
        local requireitems = require_package.load_requires(requires_str, requires_extra)
        for _, requireitem in ipairs(requireitems) do
            local requireinfo = requireitem.info or {}
            local requirename = requireinfo.alias or requireitem.name
            if requirename == packagename then
                return requireinfo
            end
        end
    end
end

-- get package toolchains envs
function _get_package_toolchains_envs(envs, package, opt)
    opt = opt or {}
    local toolchains = package:config("toolchains")
    if toolchains then

        -- pass toolchains name and their package configurations
        local toolchain_packages = {}
        local toolchains_custom = {}
        for _, name in ipairs(toolchains) do
            local toolchain_inst = toolchain.load(name, {plat = package:plat(), arch = package:arch()})
            if toolchain_inst then
                table.join2(toolchain_packages, toolchain_inst:config("packages"))
                if not toolchain_inst:is_builtin() then
                    table.insert(toolchains_custom, toolchain_inst)
                end
            end
        end
        local rcfile_path = os.tmpfile() .. ".lua"
        local rcfile = io.open(rcfile_path, 'w')
        if #toolchain_packages > 0 then
            for _, packagename in ipairs(toolchain_packages) do
                local requireinfo = _get_package_requireinfo(packagename)
                if requireinfo then
                    requireinfo.originstr = nil
                    rcfile:print("add_requires(\"%s\", %s)", packagename, string.serialize(requireinfo, {strip = true, indent = false}))
                else
                    rcfile:print("add_requires(\"%s\")", packagename)
                end
            end
        end
        rcfile:print("add_toolchains(\"%s\")", table.concat(table.wrap(toolchains), '", "'))
        rcfile:close()
        table.insert(envs.XMAKE_RCFILES, rcfile_path)

        -- pass custom toolchains definition in project
        for _, toolchain_inst in ipairs(toolchains_custom) do
            -- we must load it first
            -- @see https://github.com/xmake-io/xmake/issues/3774
            if toolchain_inst:check() then
                toolchain_inst:load()
                local toolchains_file = os.tmpfile()
                dprint("passing toolchain(%s) to %s", toolchain_inst:name(), toolchains_file)
                local ok, errors = toolchain_inst:savefile(toolchains_file)
                if not ok then
                    raise("save toolchain failed, %s", errors or "unknown")
                end
                envs.XMAKE_TOOLCHAIN_DATAFILES = envs.XMAKE_TOOLCHAIN_DATAFILES or {}
                table.insert(envs.XMAKE_TOOLCHAIN_DATAFILES, toolchains_file)
            end
        end
    end
end

-- get require paths
function _get_package_requirepaths(requirepaths, package, dep, rootpath)
    for _, plaindep in ipairs(package:plaindeps()) do
        local subpath = table.join(rootpath, plaindep:name())
        if plaindep == dep then
            table.insert(requirepaths, table.concat(subpath, "."))
        else
            _get_package_requirepaths(requirepaths, plaindep, dep, subpath)
        end
    end
end

-- get package depconfs envs
-- @see https://github.com/xmake-io/xmake/issues/3952
function _get_package_depconfs_envs(envs, package, opt)
    local policy = package:policy("package.xmake.pass_depconfs")
    if policy == nil then
        policy = project.policy("package.xmake.pass_depconfs")
    end
    if policy == false then
        return
    end
    local requireconfs = {}
    for _, dep in ipairs(package:librarydeps()) do
        local requireinfo = dep:requireinfo()
        if requireinfo and (requireinfo.override or (requireinfo.configs and not table.empty(requireinfo.configs))) then
            local requirepaths = {}
            _get_package_requirepaths(requirepaths, package, dep, {})
            if #requirepaths > 0 then
                table.insert(requireconfs, {requirepaths = requirepaths, requireinfo = requireinfo})
            end
        end
    end
    if #requireconfs > 0 then
        local rcfile_path = os.tmpfile() .. ".lua"
        local rcfile = io.open(rcfile_path, 'w')
        for _, requireconf in ipairs(requireconfs) do
            for _, requirepath in ipairs(requireconf.requirepaths) do
                rcfile:print("add_requireconfs(\"%s\", %s)", requirepath, string.serialize(requireconf.requireinfo, {strip = true, indent = false}))
            end
        end
        rcfile:close()
        table.insert(envs.XMAKE_RCFILES, rcfile_path)
    end
end

-- get the build environments
function buildenvs(package, opt)
    local envs = {XMAKE_RCFILES = {}}
    table.join2(envs.XMAKE_RCFILES, os.getenv("XMAKE_RCFILES"))
    _get_package_toolchains_envs(envs, package, opt)
    _get_package_depconfs_envs(envs, package, opt)
    -- we should avoid using $XMAKE_CONFIGDIR outside to cause conflicts
    envs.XMAKE_CONFIGDIR = os.curdir()
    envs.XMAKE_IN_XREPO  = "1"
    envs.XMAKE_IN_PROJECT_GENERATOR = ""
    return envs
end

-- install package
function install(package, configs, opt)
    opt = opt or {}

    -- copy the ported xmake.lua in the default position if it's missing
    local xmakefile = path.join(opt.curdir or os.curdir(), "xmake.lua")
    if not os.isfile(xmakefile) then
        local xmakefile_port = path.join(package:scriptdir(), "port", "xmake.lua")
        if os.isfile(xmakefile_port) then
            os.cp(xmakefile_port, xmakefile)
        end
    end

    -- get build environments
    local envs = opt.envs or buildenvs(package)

    -- pass local repositories
    for _, repo in ipairs(repository.repositories()) do
        local repo_argv = {"repo"}
        _set_builtin_argv(package, repo_argv)
        table.join2(repo_argv, {"--add", repo:name(), repo:directory()})
        os.vrunv(os.programfile(), repo_argv, {envs = envs, curdir = opt.curdir})
    end

    -- pass configurations
    -- we need to put `-P` in the first argument of _set_builtin_argv() to avoid option.parse() parsing errors
    local argv = {"f"}
    _set_builtin_argv(package, argv)
    table.insert(argv, "-y")
    table.insert(argv, "-c")
    for name, value in pairs(_get_configs(package, configs, opt)) do
        value = tostring(value):trim()
        if type(name) == "number" then
            if value ~= "" then
                table.insert(argv, value)
            end
        else
            table.insert(argv, "--" .. name .. "=" .. value)
        end
    end

    -- do configure
    os.vrunv(os.programfile(), argv, {envs = envs, curdir = opt.curdir})

    -- do build
    argv = {"build"}
    _set_builtin_argv(package, argv)
    local njob = opt.jobs or option.get("jobs")
    if njob then
        table.insert(argv, "--jobs=" .. njob)
    end
    local targets = table.wrap(opt.targets or opt.target)
    if #targets ~= 0 then
        table.join2(argv, targets)
    end
    os.vrunv(os.programfile(), argv, {envs = envs, curdir = opt.curdir})

    -- do install
    argv = {"install", "-y", "--packages=n", "-o", package:installdir()}
    _set_builtin_argv(package, argv)
    local targets = table.wrap(opt.targets or opt.target)
    if #targets ~= 0 then
        table.join2(argv, targets)
    end
    os.vrunv(os.programfile(), argv, {envs = envs, curdir = opt.curdir})
end
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        xmake.lua
--

-- imports
import("core.base.option")
import("core.base.global")
import("core.tool.toolchain")
import("core.project.project")
import("core.package.repository")
import("private.action.require.impl.package", {alias = "require_package"})
import("private.utils.toolchain", {alias = "toolchain_utils"})

-- get config from toolchains
function _get_config_from_toolchains(package, name)
    for _, toolchain_inst in ipairs(package:toolchains()) do
        local value = toolchain_inst:config(name)
        if value ~= nil then
            return value
        end
    end
end

-- is the toolchain compatible with the host?
function _is_toolchain_compatible_with_host(package)
    for _, name in ipairs(package:config("toolchains")) do
        if toolchain_utils.is_compatible_with_host(name) then
            return true
        end
    end
end

-- get configs for qt
function _get_configs_for_qt(package, configs, opt)
    local names = {"qt", "qt_sdkver"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
end

-- get configs for vcpkg
function _get_configs_for_vcpkg(package, configs, opt)
    local names = {"vcpkg"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
end

-- get configs for windows
function _get_configs_for_windows(package, configs, opt)
    local names = {"vs", "vs_toolset"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    -- pass runtimes from package configs
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)

    -- we can switch some toolchains, e.g. llvm/clang
    if package:config("toolchains") and _is_toolchain_compatible_with_host(package) then
        _get_configs_for_host_toolchain(package, configs, opt)
    end

    if not is_host("windows") then
        local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk")
        if sdkdir and #sdkdir > 0 then
            table.insert(configs, "--sdk=" .. sdkdir)
        end
    end
end

-- get configs for appleos
function _get_configs_for_appleos(package, configs, opt)
    local xcode = get_config("xcode")
    if xcode then
        table.insert(configs, "--xcode=" .. xcode)
    end
    local xcode_sdkver = get_config("xcode_sdkver")
    if xcode_sdkver then
        table.insert(configs, "--xcode_sdkver=" .. xcode_sdkver)
    end
    local target_minver = get_config("target_minver")
    if target_minver then
        table.insert(configs, "--target_minver=" .. target_minver)
    end
    local appledev = get_config("appledev")
    if appledev then
        table.insert(configs, "--appledev=" .. appledev)
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for android
function _get_configs_for_android(package, configs, opt)
    local names = {"ndk", "ndk_sdkver", "ndk_stdcxx", "ndk_cxxstl"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for mingw
function _get_configs_for_mingw(package, configs, opt)
    local names = {"mingw", "sdk", "ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for generic, e.g. linux, macosx, bsd host platforms
function _get_configs_for_generic(package, configs, opt)
    local names = {"ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
    if package:is_plat("macosx") then
        table.join2(names, "xcode", "xcode_sdkver", "target_minver", "appledev")
    end
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for host toolchain
function _get_configs_for_host_toolchain(package, configs, opt)
    local bindir = _get_config_from_toolchains(package, "bindir") or get_config("bin")
    if bindir then
        table.insert(configs, "--bin=" .. bindir)
    end
    local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk")
    if sdkdir then
        table.insert(configs, "--sdk=" .. sdkdir)
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    local toolchain_name = get_config("toolchain")
    if toolchain_name then
        table.insert(configs, "--toolchain=" .. toolchain_name)
    end
    _get_configs_for_qt(package, configs, opt)
    _get_configs_for_vcpkg(package, configs, opt)
end

-- get configs for cross
function _get_configs_for_cross(package, configs, opt)
    local cross = _get_config_from_toolchains(package, "cross") or get_config("cross")
    if cross then
        table.insert(configs, "--cross=" .. cross)
    end
    local bindir = _get_config_from_toolchains(package, "bindir") or get_config("bin")
    if bindir then
        table.insert(configs, "--bin=" .. bindir)
    end
    local sdkdir = _get_config_from_toolchains(package, "sdkdir") or get_config("sdk")
    if sdkdir then
        table.insert(configs, "--sdk=" .. sdkdir)
    end
    local runtimes = package:config("runtimes")
    if runtimes then
        table.insert(configs, "--runtimes=" .. runtimes)
    end
    local toolchain_name = get_config("toolchain")
    if toolchain_name then
        table.insert(configs, "--toolchain=" .. toolchain_name)
    end
    local names = {"ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
    for _, name in ipairs(names) do
        local value = get_config(name)
        if value ~= nil then
            table.insert(configs, "--" .. name .. "=" .. tostring(value))
        end
    end
end

-- get configs
function _get_configs(package, configs, opt)
    opt = opt or {}
    local configs  = configs or {}
    local cflags   = table.join(table.wrap(package:config("cflags")),   get_config("cflags"))
    local cxflags  = table.join(table.wrap(package:config("cxflags")),  get_config("cxflags"))
    local cxxflags = table.join(table.wrap(package:config("cxxflags")), get_config("cxxflags"))
    local asflags  = table.join(table.wrap(package:config("asflags")),  get_config("asflags"))
    local ldflags  = table.join(table.wrap(package:config("ldflags")),  get_config("ldflags"))
    local shflags  = table.join(table.wrap(package:config("shflags")),  get_config("shflags"))
    table.insert(configs, "--plat=" .. package:plat())
    table.insert(configs, "--arch=" .. package:arch())
    if configs.mode == nil then
        table.insert(configs, "--mode=" .. (package:is_debug() and "debug" or "release"))
    end
    if configs.kind == nil then
        table.insert(configs, "--kind=" .. (package:config("shared") and "shared" or "static"))
    end
    if package:is_plat("windows") then
        _get_configs_for_windows(package, configs, opt)
    elseif package:is_plat("android") then
        _get_configs_for_android(package, configs, opt)
    elseif package:is_plat("iphoneos", "watchos", "appletvos") or
        -- for cross-compilation on macOS, @see https://github.com/xmake-io/xmake/issues/2804
        (package:is_plat("macosx") and (get_config("appledev") or not package:is_arch(os.subarch()))) then
        _get_configs_for_appleos(package, configs, opt)
    elseif package:is_plat("mingw") then
        _get_configs_for_mingw(package, configs, opt)
    elseif package:is_cross() then
        _get_configs_for_cross(package, configs, opt)
    elseif package:config("toolchains") then
        -- we still need find system libraries,
        -- it just pass toolchain environments if the toolchain is compatible with host
        if _is_toolchain_compatible_with_host(package) then
            _get_configs_for_host_toolchain(package, configs, opt)
        else
            _get_configs_for_cross(package, configs, opt)
        end
    else
        _get_configs_for_generic(package, configs, opt)
    end

    local policies = get_config("policies")
    local policies_list = policies and policies:split(",") or {}
    if package:config("lto") and (not policies or not policies:find("build.optimization.lto", 1, true)) then
        table.insert(policies_list, "build.optimization.lto")
    end
    if package:config("asan") and (not policies or not policies:find("build.sanitizer.address", 1, true)) then
        table.insert(policies_list, "build.sanitizer.address")
    end
    if not package:use_external_includes() and (not policies or not policies:find("package.include_external_headers", 1, true)) then
        table.insert(policies_list, "package.include_external_headers:n")
    end
    if policies:find("package.build.ccache", 1, true) then
        table.insert(configs, "--ccachedir=" .. path.join(path.directory(package:cachedir()), "build_cache"))
        table.insert(policies_list, "build.ccache")
    end
    if policies then
        table.insert(configs, "--policies=" .. table.concat(policies_list, ","))
    end

    if not package:is_plat("windows", "mingw") and package:config("pic") ~= false then
        table.insert(cxflags, "-fPIC")
    end
    if cflags and #cflags > 0 then
        table.insert(configs, "--cflags=" .. table.concat(cflags, ' '))
    end
    if cxflags and #cxflags > 0 then
        table.insert(configs, "--cxflags=" .. table.concat(cxflags, ' '))
    end
    if cxxflags and #cxxflags > 0 then
        table.insert(configs, "--cxxflags=" .. table.concat(cxxflags, ' '))
    end
    if asflags and #asflags > 0 then
        table.insert(configs, "--asflags=" .. table.concat(asflags, ' '))
    end
    if ldflags and #ldflags > 0 then
        table.insert(configs, "--ldflags=" .. table.concat(ldflags, ' '))
    end
    if shflags and #shflags > 0 then
        table.insert(configs, "--shflags=" .. table.concat(shflags, ' '))
    end
    local builddir = opt.builddir or opt.buildir or package:builddir()
    if builddir then
        table.insert(configs, "--builddir=" .. builddir)
    end
    if opt.buildir then
        wprint("{buildir = } has been deprecated, please use {builddir = } in xmake.install")
    end
    return configs
end

-- maybe in project?
-- @see https://github.com/xmake-io/xmake/issues/3720
function _maybe_in_project(package)
    local dir = package:sourcedir() or package:cachedir()
    local parentdir = path.directory(dir)
    while parentdir and os.isdir(parentdir) do
        if os.isfile(path.join(parentdir, "xmake.lua")) then
            return true
        end
        parentdir = path.directory(parentdir)
    end
end

-- set some builtin global options from the parent xmake
function _set_builtin_argv(package, argv)
    -- if the package cache directory is modified,
    -- we need to force the project directory to be specified to avoid interference by the upper level xmake.lua.
    -- and we also need to put `-P` in the first argument to avoid option.parse() parsing errors
    if _maybe_in_project(package) then
        table.insert(argv, "-P")
        table.insert(argv, os.curdir())
    end
    for _, name in ipairs({"diagnosis", "verbose", "quiet", "yes", "confirm", "root"}) do
        local value = option.get(name)
        if type(value) == "boolean" then
            table.insert(argv, "--" .. name)
        elseif value ~= nil then
            table.insert(argv, "--" .. name .. "=" .. value)
        end
    end
end

-- get require info of package
function _get_package_requireinfo(packagename)
    if os.isfile(os.projectfile()) then
        local requires_str, requires_extra = project.requires_str()
        local requireitems = require_package.load_requires(requires_str, requires_extra)
        for _, requireitem in ipairs(requireitems) do
            local requireinfo = requireitem.info or {}
            local requirename = requireinfo.alias or requireitem.name
            if requirename == packagename then
                return requireinfo
            end
        end
    end
end

-- get package toolchains envs
function _get_package_toolchains_envs(envs, package, opt)
    opt = opt or {}
    local toolchains = package:config("toolchains")
    if toolchains then

        -- pass toolchains name and their package configurations
        local toolchain_packages = {}
        local toolchains_custom = {}
        for _, name in ipairs(toolchains) do
            local toolchain_inst = toolchain.load(name, {plat = package:plat(), arch = package:arch()})
            if toolchain_inst then
                table.join2(toolchain_packages, toolchain_inst:config("packages"))
                if not toolchain_inst:is_builtin() then
                    table.insert(toolchains_custom, toolchain_inst)
                end
            end
        end
        local rcfile_path = os.tmpfile() .. ".lua"
        local rcfile = io.open(rcfile_path, 'w')
        if #toolchain_packages > 0 then
            for _, packagename in ipairs(toolchain_packages) do
                local requireinfo = _get_package_requireinfo(packagename)
                if requireinfo then
                    requireinfo.originstr = nil
                    rcfile:print("add_requires(\"%s\", %s)", packagename, string.serialize(requireinfo, {strip = true, indent = false}))
                else
                    rcfile:print("add_requires(\"%s\")", packagename)
                end
            end
        end
        rcfile:print("add_toolchains(\"%s\")", table.concat(table.wrap(toolchains), '", "'))
        rcfile:close()
        table.insert(envs.XMAKE_RCFILES, rcfile_path)

        -- pass custom toolchains definition in project
        for _, toolchain_inst in ipairs(toolchains_custom) do
            -- we must load it first
            -- @see https://github.com/xmake-io/xmake/issues/3774
            if toolchain_inst:check() then
                toolchain_inst:load()
                local toolchains_file = os.tmpfile()
                dprint("passing toolchain(%s) to %s", toolchain_inst:name(), toolchains_file)
                local ok, errors = toolchain_inst:savefile(toolchains_file)
                if not ok then
                    raise("save toolchain failed, %s", errors or "unknown")
                end
                envs.XMAKE_TOOLCHAIN_DATAFILES = envs.XMAKE_TOOLCHAIN_DATAFILES or {}
                table.insert(envs.XMAKE_TOOLCHAIN_DATAFILES, toolchains_file)
            end
        end
    end
end

-- get require paths
function _get_package_requirepaths(requirepaths, package, dep, rootpath)
    for _, plaindep in ipairs(package:plaindeps()) do
        local subpath = table.join(rootpath, plaindep:name())
        if plaindep == dep then
            table.insert(requirepaths, table.concat(subpath, "."))
        else
            _get_package_requirepaths(requirepaths, plaindep, dep, subpath)
        end
    end
end

-- get package depconfs envs
-- @see https://github.com/xmake-io/xmake/issues/3952
function _get_package_depconfs_envs(envs, package, opt)
    local policy = package:policy("package.xmake.pass_depconfs")
    if policy == nil then
        policy = project.policy("package.xmake.pass_depconfs")
    end
    if policy == false then
        return
    end
    local requireconfs = {}
    for _, dep in ipairs(package:librarydeps()) do
        local requireinfo = dep:requireinfo()
        if requireinfo and (requireinfo.override or (requireinfo.configs and not table.empty(requireinfo.configs))) then
            local requirepaths = {}
            _get_package_requirepaths(requirepaths, package, dep, {})
            if #requirepaths > 0 then
                table.insert(requireconfs, {requirepaths = requirepaths, requireinfo = requireinfo})
            end
        end
    end
    if #requireconfs > 0 then
        local rcfile_path = os.tmpfile() .. ".lua"
        local rcfile = io.open(rcfile_path, 'w')
        for _, requireconf in ipairs(requireconfs) do
            for _, requirepath in ipairs(requireconf.requirepaths) do
                rcfile:print("add_requireconfs(\"%s\", %s)", requirepath, string.serialize(requireconf.requireinfo, {strip = true, indent = false}))
            end
        end
        rcfile:close()
        table.insert(envs.XMAKE_RCFILES, rcfile_path)
    end
end

-- get the build environments
function buildenvs(package, opt)
    local envs = {XMAKE_RCFILES = {}}
    table.join2(envs.XMAKE_RCFILES, os.getenv("XMAKE_RCFILES"))
    _get_package_toolchains_envs(envs, package, opt)
    _get_package_depconfs_envs(envs, package, opt)
    -- we should avoid using $XMAKE_CONFIGDIR outside to cause conflicts
    envs.XMAKE_CONFIGDIR = os.curdir()
    envs.XMAKE_IN_XREPO  = "1"
    envs.XMAKE_IN_PROJECT_GENERATOR = ""
    return envs
end

-- install package
function install(package, configs, opt)
    opt = opt or {}

    -- copy the ported xmake.lua in the default position if it's missing
    local xmakefile = path.join(opt.curdir or os.curdir(), "xmake.lua")
    if not os.isfile(xmakefile) then
        local xmakefile_port = path.join(package:scriptdir(), "port", "xmake.lua")
        if os.isfile(xmakefile_port) then
            os.cp(xmakefile_port, xmakefile)
        end
    end

    -- get build environments
    local envs = opt.envs or buildenvs(package)

    -- pass local repositories
    for _, repo in ipairs(repository.repositories()) do
        local repo_argv = {"repo"}
        _set_builtin_argv(package, repo_argv)
        table.join2(repo_argv, {"--add", repo:name(), repo:directory()})
        os.vrunv(os.programfile(), repo_argv, {envs = envs, curdir = opt.curdir})
    end

    -- pass configurations
    -- we need to put `-P` in the first argument of _set_builtin_argv() to avoid option.parse() parsing errors
    local argv = {"f"}
    _set_builtin_argv(package, argv)
    table.insert(argv, "-y")
    table.insert(argv, "-c")
    for name, value in pairs(_get_configs(package, configs, opt)) do
        value = tostring(value):trim()
        if type(name) == "number" then
            if value ~= "" then
                table.insert(argv, value)
            end
        else
            table.insert(argv, "--" .. name .. "=" .. value)
        end
    end

    -- do configure
    os.vrunv(os.programfile(), argv, {envs = envs, curdir = opt.curdir})

    -- do build
    argv = {"build"}
    _set_builtin_argv(package, argv)
    local njob = opt.jobs or option.get("jobs")
    if njob then
        table.insert(argv, "--jobs=" .. njob)
    end
    local targets = table.wrap(opt.targets or opt.target)
    if #targets ~= 0 then
        table.join2(argv, targets)
    end
    os.vrunv(os.programfile(), argv, {envs = envs, curdir = opt.curdir})

    -- do install
    argv = {"install", "-y", "--packages=n", "-o", package:installdir()}
    _set_builtin_argv(package, argv)
    local targets = table.wrap(opt.targets or opt.target)
    if #targets ~= 0 then
        table.join2(argv, targets)
    end
    os.vrunv(os.programfile(), argv, {envs = envs, curdir = opt.curdir})
end