|
root / src / commands / build.reef
build.reef Reef 1292 lines 41.7 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
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
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
/******************************************************************************
               __               ____                __
              / /   ___  ____ _/ __/_____________ _/ /__
             / /   / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \
            / /___/  __/ /_/ / __(__  ) /__/ /_/ / /  __/
           /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/

   (C)opyright 2025, Leafscale, LLC -  https://www.leafscale.com

   Project: Zygaena
  Filename: build.reef
   Authors: Chris Tusa <chris.tusa@leafscale.com>
   License: <see LICENSE file included with this source code>
Description: Build command - compile packages from ports

******************************************************************************/

module commands.build

import sys.args
import sys.process
import io.file
import io.dir
import io.path
import io.console
import core.str
import core.port
import core.config
import core.database
import core.resolver
import util.version as ver
import types
import util.color
import util.prompt
import util.checksum
import util.archive
import util.mtree
import util.http as uhttp
import exitcodes as ec

export
    fn execute(opts: types.GlobalOptions): int
end export

// Build context
type BuildContext = struct
    port: types.Port
    work_dir: string
    pkg_dir: string
    src_dir: string
    cfg: config.Config
    noclean: bool
    force_clean: bool
    log_file: string
    logging: bool
    quiet: bool
end BuildContext

// Load a port from path or find it by name
fn load_or_find_port(port_path: string): types.PortResult
    // Check if it's a valid port directory (must contain package.toml)
    // This prevents confusion with binary files or other directories with same name
    if dir.dir_exists(port_path)
        let toml_path = path.join_path(port_path, "package.toml")
        if file.fileExists(toml_path)
            color.print_action("Loading port from: " + port_path)
            return port.load(port_path)
        end if
    end if

    // Not a valid port directory, search by name
    color.print_action("Finding port: " + port_path)
    return port.find(port_path)
end load_or_find_port

fn execute(opts: types.GlobalOptions): int
    // Parse arguments
    let argc = args.count()

    // Port argument should be after the command
    if argc <= opts.cmd_index + 1
        print_usage()
        return ec.EXIT_USAGE()
    end if

    // Parse flags
    mut noclean = false
    mut force_clean = false
    mut quiet_flag = false
    mut verbose_flag = false
    mut install_flag = false
    mut port_path = ""

    mut i = opts.cmd_index + 1
    while i < argc
        let arg = args.get(i)
        // Skip global flags with values
        if arg == "--root" or arg == "--prefix"
            i = i + 2
            continue
        end if
        if arg == "--noclean" or arg == "-n"
            noclean = true
        elif arg == "--clean" or arg == "-c"
            force_clean = true
        elif arg == "--quiet" or arg == "-q"
            quiet_flag = true
        elif arg == "--verbose" or arg == "-v"
            verbose_flag = true
        elif arg == "--install"
            install_flag = true
        elif arg == "--no-install"
            install_flag = false
        elif not str.starts_with(arg, "-")
            if str.length(port_path) == 0
                port_path = arg
            end if
        end if
        i = i + 1
    end while

    if str.length(port_path) == 0
        print_usage()
        return ec.EXIT_USAGE()
    end if

    // Handle "." for current directory
    if port_path == "."
        port_path = dir.current_dir()
    end if

    // Load configuration
    let cfg = config.load()
    uhttp.enable_proxy(config.get_use_proxy(cfg))

    // Try to find the port by name if path doesn't exist
    let result = load_or_find_port(port_path)

    if not result.success
        color.print_error(result.error)
        return ec.EXIT_PKG_NOT_FOUND()
    end if

    let p = result.port
    color.print_success("Loaded port: " + p.info.name + " " + p.info.version)

    // Dry-run: show build plan without executing
    if opts.dry_run
        color.print_info("Dry run — would build: " + p.info.name + " " + p.info.version)
        let dry_resolve = resolver.resolve_build_deps(p.info.name)
        if dry_resolve.success and dry_resolve.count > 1
            color.print_info("Dependencies to build first:")
            mut di = 0
            while di < dry_resolve.count - 1
                let dep_port = port.find(dry_resolve.packages[di])
                if dep_port.success
                    color.print_info("  " + dry_resolve.packages[di] + " " + dep_port.port.info.version)
                else
                    color.print_info("  " + dry_resolve.packages[di])
                end if
                di = di + 1
            end while
        end if
        color.print_info("Dry run — no changes made")
        return ec.EXIT_SUCCESS()
    end if

    // Resolve, build, and install missing dependencies before building
    if not resolve_and_build_deps(p, opts, cfg)
        // Check if the failure was due to circular dependencies
        let check_result = resolver.resolve_build_deps(p.info.name)
        if not check_result.success and str.starts_with(check_result.error, "Circular")
            color.print_error("Cannot build " + p.info.name + " — circular dependency")
            return ec.EXIT_DEPS_CIRCULAR()
        end if
        color.print_error("Cannot build " + p.info.name + " — missing dependencies")
        return ec.EXIT_DEPS_UNMET()
    end if

    // Determine quiet mode: CLI flag overrides config, verbose overrides quiet
    mut quiet_mode = config.get_quiet(cfg)
    if quiet_flag
        quiet_mode = true
    end if
    if verbose_flag
        quiet_mode = false
    end if

    // Create build context
    let ctx = create_context(p, cfg, noclean, force_clean, quiet_mode)

    // Step 1: Setup directories
    if not setup_directories(ctx)
        color.print_error("Failed to create build directories")
        return ec.EXIT_ERROR()
    end if

    // Initialize logging after directories are created
    init_log(ctx)
    if ctx.logging and str.length(ctx.log_file) > 0
        log_info(ctx, "Log file: " + ctx.log_file)
    end if

    // Step 2: Download sources
    if not download_sources(ctx)
        log_error(ctx, "Failed to download sources")
        print_log_location(ctx)
        cleanup_build(ctx)
        return ec.EXIT_DOWNLOAD_FAILED()
    end if

    // Step 3: Verify checksums
    if not verify_checksums(ctx)
        log_error(ctx, "Checksum verification failed")
        print_log_location(ctx)
        cleanup_build(ctx)
        return ec.EXIT_CHECKSUM_FAILED()
    end if

    // Step 4: Extract sources
    if not extract_sources(ctx)
        log_error(ctx, "Failed to extract sources")
        print_log_location(ctx)
        cleanup_build(ctx)
        return ec.EXIT_EXTRACT_FAILED()
    end if

    // Step 5: Apply patches
    if not apply_patches(ctx)
        log_error(ctx, "Failed to apply patches")
        print_log_location(ctx)
        cleanup_build(ctx)
        return ec.EXIT_PATCH_FAILED()
    end if

    // Step 6: Run build script
    if not run_build(ctx)
        log_error(ctx, "Build failed")
        print_log_location(ctx)
        cleanup_build(ctx)
        return ec.EXIT_BUILD_FAILED()
    end if

    // Step 7: Compress man pages (if enabled)
    if config.get_compress_man(ctx.cfg)
        compress_man_pages(ctx)
    end if

    // Step 8: Create package
    if not create_package(ctx)
        log_error(ctx, "Failed to create package")
        print_log_location(ctx)
        cleanup_build(ctx)
        return ec.EXIT_PACKAGE_FAILED()
    end if

    // Success!
    let pkg_name = p.info.name + "-" + p.info.version + "-" + int_to_str(p.info.release) + ".pkg.tar.xz"
    let pkg_path = path.join_path(config.get_packages_dir(cfg), pkg_name)
    log_success(ctx, "Package created: " + pkg_path)

    // Cleanup work directory on success
    cleanup_build(ctx)

    // Step 9: Install the built package (only with --install)
    if install_flag
        color.print_action("Installing " + p.info.name + "...")
        let self_exe = args.get(0)
        let install_cmd = self_exe + " install -y -f \"" + pkg_path + "\""
        let install_pid = process.process_spawn_shell(install_cmd)
        if install_pid < 0
            color.print_error("Failed to launch installer for " + p.info.name)
            return ec.EXIT_INSTALL_FAILED()
        end if
        let install_exit = process.process_wait(install_pid)
        if install_exit != 0
            color.print_error("Failed to install " + p.info.name)
            return ec.EXIT_INSTALL_FAILED()
        end if
    else
        color.print_success("Package ready (use --install to install, or: coral install \"" + pkg_path + "\")")
    end if

    return ec.EXIT_SUCCESS()
end execute

proc print_usage()
    println("Usage: coral build [options] <port-path>")
    println("")
    println("Build a package from a port directory.")
    println("")
    println("Arguments:")
    println("  <port-path>    Path to port directory (use . for current)")
    println("")
    println("Options:")
    println("  --install      Install the package after building")
    println("  --no-install   Build only, do not install (default)")
    println("  -n, --noclean  Don't clean work/pkg directories after build")
    println("  -c, --clean    Force cleanup even if disabled in config")
    println("  -q, --quiet    Hide subprocess output (still logged to file)")
    println("  -v, --verbose  Show all output (overrides quiet)")
    println("")
    println("Examples:")
    println("  coral build /usr/zports/base/vim")
    println("  coral build --install base/vim")
    println("  coral build --noclean .")
    println("  coral build --quiet vim")
end print_usage

// Resolve all build dependencies recursively, build from source, and install each.
// Returns true if all dependencies are satisfied (either already installed or built+installed).
fn resolve_and_build_deps(p: types.Port, opts: types.GlobalOptions, cfg: config.Config): bool
    // Resolve the full dependency tree (runtime + build deps, recursive)
    let resolve_result = resolver.resolve_build_deps(p.info.name)

    if not resolve_result.success
        color.print_error("Dependency resolution failed: " + resolve_result.error)
        return false
    end if

    // The result includes the target package as the last element — strip it
    mut dep_count = resolve_result.count
    if dep_count > 0
        let last = resolve_result.packages[dep_count - 1]
        if str.equals(last, p.info.name)
            dep_count = dep_count - 1
        end if
    end if

    // No deps to build
    if dep_count == 0
        return true
    end if

    // Look up version info for each dep to display in the build plan
    mut dep_versions: [string] = new [string](256)
    mut i = 0
    while i < dep_count
        let dep_port_result = port.find(resolve_result.packages[i])
        if dep_port_result.success
            dep_versions[i] = dep_port_result.port.info.version
        else
            dep_versions[i] = ""
        end if
        i = i + 1
    end while

    // Show build plan and ask for confirmation
    if not prompt.confirm_build_plan(resolve_result.packages, dep_versions, dep_count, p.info.name, opts)
        color.print_info("Build cancelled")
        return false
    end if

    // Build and install each dependency in topological order
    i = 0
    while i < dep_count
        let dep_name = resolve_result.packages[i]
        println("")
        color.print_action("[" + int_to_str(i + 1) + "/" + int_to_str(dep_count) + "] Building dependency: " + dep_name)

        // Build the dependency from source
        if not build_single_port(dep_name, cfg, opts)
            color.print_error("Failed to build dependency: " + dep_name)
            return false
        end if

        // Install the built package
        let dep_port_result = port.find(dep_name)
        if not dep_port_result.success
            color.print_error("Cannot find port after build: " + dep_name)
            return false
        end if

        let dp = dep_port_result.port
        let pkg_filename = dp.info.name + "-" + dp.info.version + "-" + int_to_str(dp.info.release) + ".pkg.tar.xz"
        let pkg_path = path.join_path(config.get_packages_dir(cfg), pkg_filename)

        if not file.fileExists(pkg_path)
            color.print_error("Built package not found: " + pkg_path)
            return false
        end if

        color.print_action("Installing " + dep_name + "...")
        let self_exe = args.get(0)
        let install_cmd = self_exe + " install -y \"" + pkg_path + "\""
        let pid = process.process_spawn_shell(install_cmd)
        if pid < 0
            color.print_error("Failed to launch installer for " + dep_name)
            return false
        end if
        let exit_code = process.process_wait(pid)
        if exit_code != 0
            color.print_error("Failed to install " + dep_name)
            return false
        end if

        color.print_success("Dependency ready: " + dep_name)
        i = i + 1
    end while

    println("")
    color.print_success("All " + int_to_str(dep_count) + " dependencies built and installed")
    return true
end resolve_and_build_deps

// Build a single port from source. Used for dependency builds.
// Returns true if build succeeds and package archive is created.
fn build_single_port(name: string, cfg: config.Config, opts: types.GlobalOptions): bool
    let port_result = port.find(name)
    if not port_result.success
        color.print_error("Port not found: " + name)
        return false
    end if

    let p = port_result.port

    // Determine quiet mode
    mut quiet_mode = config.get_quiet(cfg)
    if opts.verbose
        quiet_mode = false
    end if
    if opts.quiet
        quiet_mode = true
    end if

    let ctx = create_context(p, cfg, false, false, quiet_mode)

    // Run the full build pipeline
    if not setup_directories(ctx)
        color.print_error("Failed to create build directories for " + name)
        return false
    end if

    init_log(ctx)

    if not download_sources(ctx)
        log_error(ctx, "Failed to download sources for " + name)
        print_log_location(ctx)
        cleanup_build(ctx)
        return false
    end if

    if not verify_checksums(ctx)
        log_error(ctx, "Checksum verification failed for " + name)
        print_log_location(ctx)
        cleanup_build(ctx)
        return false
    end if

    if not extract_sources(ctx)
        log_error(ctx, "Failed to extract sources for " + name)
        print_log_location(ctx)
        cleanup_build(ctx)
        return false
    end if

    if not apply_patches(ctx)
        log_error(ctx, "Failed to apply patches for " + name)
        print_log_location(ctx)
        cleanup_build(ctx)
        return false
    end if

    if not run_build(ctx)
        log_error(ctx, "Build failed for " + name)
        print_log_location(ctx)
        cleanup_build(ctx)
        return false
    end if

    // Compress man pages (if enabled)
    if config.get_compress_man(cfg)
        compress_man_pages(ctx)
    end if

    if not create_package(ctx)
        log_error(ctx, "Failed to create package for " + name)
        print_log_location(ctx)
        cleanup_build(ctx)
        return false
    end if

    let pkg_filename = p.info.name + "-" + p.info.version + "-" + int_to_str(p.info.release) + ".pkg.tar.xz"
    let pkg_path = path.join_path(config.get_packages_dir(cfg), pkg_filename)
    log_success(ctx, "Package created: " + pkg_path)

    cleanup_build(ctx)
    return true
end build_single_port

fn create_context(p: types.Port, cfg: config.Config, noclean: bool, force_clean: bool, quiet: bool): BuildContext
    // New directory structure:
    //   /usr/zports/pkgs/build/<pkgname>/src/   - extracted sources, compilation
    //   /usr/zports/pkgs/build/<pkgname>/stage/ - DESTDIR for make install
    let build_base = config.get_build_dir(cfg)
    let pkg_build_dir = path.join_path(build_base, p.info.name)
    let work_dir = path.join_path(pkg_build_dir, "src")
    let pkg_dir = path.join_path(pkg_build_dir, "stage")

    // Generate log file path with timestamp (in package build root, not src/)
    let logging_enabled = config.get_logging(cfg)
    mut log_path = ""
    if logging_enabled
        log_path = path.join_path(pkg_build_dir, "coral-build-" + p.info.name + "-" + get_timestamp() + ".log")
    end if

    return BuildContext{
        port: p,
        work_dir: work_dir,
        pkg_dir: pkg_dir,
        src_dir: "",
        cfg: cfg,
        noclean: noclean,
        force_clean: force_clean,
        log_file: log_path,
        logging: logging_enabled,
        quiet: quiet
    }
end create_context

// Get current timestamp for log filename
fn get_timestamp(): string
    // Use date command to get timestamp
    let tmp_file = "/tmp/coral_ts_" + int_to_str(process.getpid()) + ".txt"
    let cmd = "date '+%Y%m%d-%H%M%S' > \"" + tmp_file + "\""
    let pid = process.process_spawn_shell(cmd)
    if pid > 0
        process.process_wait(pid)
    end if

    let ts = file.readFile(tmp_file)

    // Cleanup
    let rm_cmd = "rm -f \"" + tmp_file + "\""
    let rm_pid = process.process_spawn_shell(rm_cmd)
    if rm_pid > 0
        process.process_wait(rm_pid)
    end if

    return str.trim(ts, '\n')
end get_timestamp

// Initialize log file with header
proc init_log(ctx: BuildContext)
    if not ctx.logging
        return
    end if

    mut header = "=== Coral Build Log ===\n"
    header = header + "Package: " + ctx.port.info.name + "\n"
    header = header + "Version: " + ctx.port.info.version + "\n"
    header = header + "Work Dir: " + ctx.work_dir + "\n"
    header = header + "Log File: " + ctx.log_file + "\n"
    header = header + "========================\n\n"

    file.writeFile(ctx.log_file, header)
end init_log

// Log a message to file and optionally to screen
proc log_msg(ctx: BuildContext, msg: string, show_screen: bool)
    // Always write to log file if logging enabled
    if ctx.logging and str.length(ctx.log_file) > 0
        file.appendFile(ctx.log_file, msg + "\n")
    end if

    // Show on screen unless quiet mode suppresses it
    if show_screen
        println(msg)
    end if
end log_msg

// Log info message (always show on screen)
proc log_info(ctx: BuildContext, msg: string)
    if ctx.logging and str.length(ctx.log_file) > 0
        file.appendFile(ctx.log_file, "[INFO] " + msg + "\n")
    end if
    color.print_info(msg)
end log_info

// Log success message (always show on screen)
proc log_success(ctx: BuildContext, msg: string)
    if ctx.logging and str.length(ctx.log_file) > 0
        file.appendFile(ctx.log_file, "[OK] " + msg + "\n")
    end if
    color.print_success(msg)
end log_success

// Log error message (always show on screen)
proc log_error(ctx: BuildContext, msg: string)
    if ctx.logging and str.length(ctx.log_file) > 0
        file.appendFile(ctx.log_file, "[ERROR] " + msg + "\n")
    end if
    color.print_error(msg)
end log_error

// Log action message (always show on screen)
proc log_action(ctx: BuildContext, msg: string)
    if ctx.logging and str.length(ctx.log_file) > 0
        file.appendFile(ctx.log_file, "[ACTION] " + msg + "\n")
    end if
    color.print_action(msg)
end log_action

// Log warning message (always show on screen)
proc log_warning(ctx: BuildContext, msg: string)
    if ctx.logging and str.length(ctx.log_file) > 0
        file.appendFile(ctx.log_file, "[WARN] " + msg + "\n")
    end if
    color.print_warning(msg)
end log_warning

// Print log file location (for debugging failed builds)
proc print_log_location(ctx: BuildContext)
    if ctx.logging and str.length(ctx.log_file) > 0
        color.print_info("Build log: " + ctx.log_file)
    end if
end print_log_location

// Run a shell command with output captured to log
// Returns exit code
fn run_logged_cmd(ctx: BuildContext, cmd: string): int
    if ctx.logging and str.length(ctx.log_file) > 0
        file.appendFile(ctx.log_file, "[CMD] " + cmd + "\n")
    end if

    // Build command that logs output to file
    // Redirects all output to log file to avoid tee pipeline issues
    // (tee caused SIGPIPE on long builds and masked exit codes)
    mut full_cmd = cmd
    if ctx.logging and str.length(ctx.log_file) > 0
        // Both quiet and normal mode: log to file only
        // Real-time terminal output is sacrificed for build reliability
        full_cmd = "(" + cmd + ") >> \"" + ctx.log_file + "\" 2>&1"
    else
        if ctx.quiet
            // No logging, quiet mode: suppress output
            full_cmd = "(" + cmd + ") >/dev/null 2>&1"
        end if
        // No logging, not quiet: just run normally
    end if

    // Flush stdout before spawning subprocess to ensure proper output ordering
    console.flushOutput()

    let pid = process.process_spawn_shell(full_cmd)
    if pid < 0
        return -1
    end if

    return process.process_wait(pid)
end run_logged_cmd

fn setup_directories(ctx: BuildContext): bool
    color.print_info("Setting up build directories...")

    // Create work directory
    if dir.dir_exists(ctx.work_dir)
        // Clean existing work dir
        let cmd = "rm -rf \"" + ctx.work_dir + "\""
        let pid = process.process_spawn_shell(cmd)
        if pid > 0
            process.process_wait(pid)
        end if
    end if

    if not dir.create_dir_all(ctx.work_dir)
        return false
    end if

    // Create pkg directory (for DESTDIR)
    if dir.dir_exists(ctx.pkg_dir)
        let cmd = "rm -rf \"" + ctx.pkg_dir + "\""
        let pid = process.process_spawn_shell(cmd)
        if pid > 0
            process.process_wait(pid)
        end if
    end if

    if not dir.create_dir_all(ctx.pkg_dir)
        return false
    end if

    // Ensure sources directory exists
    let sources_dir = config.get_sources_dir(ctx.cfg)
    if not dir.dir_exists(sources_dir)
        if not dir.create_dir_all(sources_dir)
            return false
        end if
    end if

    // Ensure packages directory exists
    let packages_dir = config.get_packages_dir(ctx.cfg)
    if not dir.dir_exists(packages_dir)
        if not dir.create_dir_all(packages_dir)
            return false
        end if
    end if

    return true
end setup_directories

fn download_sources(ctx: BuildContext): bool
    if ctx.port.sources.count == 0
        color.print_info("No sources to download")
        return true
    end if

    color.print_info("Downloading sources...")

    let sources_dir = config.get_sources_dir(ctx.cfg)

    mut i = 0
    while i < ctx.port.sources.count
        let src = ctx.port.sources.sources[i]
        let filename = src.file
        let dest = path.join_path(sources_dir, filename)

        // Check if already cached with valid checksum
        if file.fileExists(dest)
            if verify_single_checksum(dest, src.checksum)
                color.print_info("Using cached: " + filename)
                i = i + 1
                continue
            else
                // Cached file has wrong checksum, re-download
                color.print_warning("Cached file checksum mismatch, re-downloading: " + filename)
            end if
        end if

        // Try each mirror URL in order
        mut download_success = false
        mut url_idx = 0
        while url_idx < src.url_count
            let url = src.urls[url_idx]

            if url_idx > 0
                color.print_info("Trying mirror: " + url)
            end if

            // Check if this is a local file (no http:// or https:// prefix)
            if is_local_source(url)
                // Local file - copy from port directory to sources cache
                if copy_local_source(url, dest, ctx.port.port_dir)
                    if verify_single_checksum(dest, src.checksum)
                        download_success = true
                        break
                    else
                        color.print_warning("Checksum mismatch for local file, trying next...")
                        let rm_cmd = "rm -f \"" + dest + "\""
                        let rm_pid = process.process_spawn_shell(rm_cmd)
                        if rm_pid > 0
                            process.process_wait(rm_pid)
                        end if
                    end if
                end if
            else
                // Remote URL - download via HTTP
                if uhttp.download(url, dest)
                    // Verify checksum after download
                    if verify_single_checksum(dest, src.checksum)
                        download_success = true
                        break
                    else
                        color.print_warning("Checksum mismatch from mirror, trying next...")
                        // Delete bad file
                        let rm_cmd = "rm -f \"" + dest + "\""
                        let rm_pid = process.process_spawn_shell(rm_cmd)
                        if rm_pid > 0
                            process.process_wait(rm_pid)
                        end if
                    end if
                end if
            end if

            url_idx = url_idx + 1
        end while

        if not download_success
            color.print_error("Failed to download: " + filename + " (all sources failed)")
            return false
        end if

        i = i + 1
    end while

    return true
end download_sources

// Check if a URL is a local file path (not http:// or https://)
fn is_local_source(url: string): bool
    if str.starts_with(url, "http://")
        return false
    end if
    if str.starts_with(url, "https://")
        return false
    end if
    return true
end is_local_source

// Copy a local source file from port directory to sources cache
fn copy_local_source(source_path: string, dest: string, port_dir: string): bool
    // Resolve the source path
    mut full_path = source_path

    // If it's a relative path, resolve relative to port directory
    if not str.starts_with(source_path, "/")
        full_path = path.join_path(port_dir, source_path)
    end if

    // Check if file exists
    if not file.fileExists(full_path)
        color.print_error("Local source file not found: " + full_path)
        return false
    end if

    color.print_info("Copying local source: " + source_path)

    // Copy the file
    let cmd = "cp \"" + full_path + "\" \"" + dest + "\""
    let pid = process.process_spawn_shell(cmd)
    if pid < 0
        return false
    end if

    let exit_code = process.process_wait(pid)
    if exit_code != 0
        color.print_error("Failed to copy local source: " + source_path)
        return false
    end if

    color.print_success("Copied: " + source_path)
    return true
end copy_local_source

// Verify a single file's checksum
fn verify_single_checksum(file_path: string, expected: string): bool
    // Handle special checksum values
    if str.equals(expected, "SKIP") or str.equals(expected, "BOOTSTRAP") or str.equals(expected, "skip")
        return true
    end if

    if str.length(expected) == 0
        return true
    end if

    return checksum.verify_checksum_with_algo(file_path, expected)
end verify_single_checksum

fn verify_checksums(ctx: BuildContext): bool
    if ctx.port.sources.count == 0
        return true
    end if

    color.print_info("Verifying checksums...")

    let sources_dir = config.get_sources_dir(ctx.cfg)

    mut i = 0
    while i < ctx.port.sources.count
        let src = ctx.port.sources.sources[i]
        let filename = src.file
        let file_path = path.join_path(sources_dir, filename)
        let expected = src.checksum

        // Handle special checksum values
        if str.equals(expected, "SKIP") or str.equals(expected, "BOOTSTRAP") or str.equals(expected, "skip")
            color.print_warning("Checksum verification skipped: " + filename)
        elif str.length(expected) > 0
            if not checksum.verify_checksum_with_algo(file_path, expected)
                color.print_error("Checksum mismatch: " + filename)
                return false
            end if
            color.print_success("Checksum verified: " + filename)
        else
            color.print_warning("No checksum for: " + filename)
        end if

        i = i + 1
    end while

    return true
end verify_checksums

fn extract_sources(ctx: BuildContext): bool
    if ctx.port.sources.count == 0
        return true
    end if

    color.print_info("Extracting sources...")

    let sources_dir = config.get_sources_dir(ctx.cfg)

    mut i = 0
    while i < ctx.port.sources.count
        let src = ctx.port.sources.sources[i]
        let filename = src.file
        let archive_path = path.join_path(sources_dir, filename)

        // Check extract flag
        if not src.extract
            // Copy file to work_dir instead of extracting (for patches, etc.)
            let dest_path = path.join_path(ctx.work_dir, filename)
            let cp_cmd = "cp \"" + archive_path + "\" \"" + dest_path + "\""
            let cp_pid = process.process_spawn_shell(cp_cmd)
            if cp_pid > 0
                let exit_code = process.process_wait(cp_pid)
                if exit_code != 0
                    color.print_error("Failed to copy: " + filename)
                    return false
                end if
            end if
            color.print_info("Copied (no extract): " + filename)
            i = i + 1
            continue
        end if

        if not archive.extract(archive_path, ctx.work_dir)
            color.print_error("Failed to extract: " + filename)
            return false
        end if

        color.print_success("Extracted: " + filename)
        i = i + 1
    end while

    return true
end extract_sources

fn apply_patches(ctx: BuildContext): bool
    if ctx.port.patches.count == 0
        return true
    end if

    log_info(ctx, "Applying patches...")

    let strip = ctx.port.patches.strip

    // Find the source directory (first subdirectory in work_dir)
    let find_srcdir = "cd \"" + ctx.work_dir + "\" && ls -d */ 2>/dev/null | head -1 | tr -d '/'"

    mut i = 0
    while i < ctx.port.patches.count
        let patch_file = ctx.port.patches.files[i]
        let patch_path = path.join_path(ctx.port.port_dir, patch_file)

        if not file.fileExists(patch_path)
            log_error(ctx, "Patch file not found: " + patch_file)
            return false
        end if

        // Apply patch from within the source directory
        let cmd = "cd \"" + ctx.work_dir + "/$(" + find_srcdir + ")\" && patch -p" + int_to_str(strip) + " < \"" + patch_path + "\""
        let exit_code = run_logged_cmd(ctx, cmd)
        if exit_code != 0
            log_error(ctx, "Failed to apply patch: " + patch_file)
            return false
        end if

        log_success(ctx, "Applied patch: " + patch_file)
        i = i + 1
    end while

    return true
end apply_patches

fn run_build(ctx: BuildContext): bool
    log_action(ctx, "Building " + ctx.port.info.name + "...")

    // Look for build.sh in port directory
    let build_script = path.join_path(ctx.port.port_dir, "build.sh")

    if not file.fileExists(build_script)
        log_error(ctx, "No build.sh found in port directory")
        return false
    end if

    // Set up environment
    let destdir = ctx.pkg_dir
    let cflags = config.get_cflags(ctx.cfg)
    let cxxflags = config.get_cxxflags(ctx.cfg)
    let jobs = config.get_jobs(ctx.cfg)

    mut jobs_str = ""
    if jobs > 0
        jobs_str = int_to_str(jobs)
    else
        // Auto-detect: use getconf (portable across illumos/Linux, unlike nproc)
        jobs_str = "$(getconf NPROCESSORS_ONLN 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)"
    end if

    // Find the extracted source directory (first directory in work_dir)
    // Typically the tarball extracts to a subdirectory like "zlib-1.3.1"
    let find_srcdir_cmd = "cd \"" + ctx.work_dir + "\" && ls -d */ 2>/dev/null | head -1 | tr -d '/'"

    // Determine PREFIX: package.toml override → coral.conf → default (/usr)
    mut prefix = config.get_prefix(ctx.cfg)
    if str.length(ctx.port.prefix) > 0
        prefix = ctx.port.prefix
    end if

    // Determine SYSCONFDIR: package.toml override → coral.conf → default (/etc)
    mut sysconfdir = config.get_sysconfdir(ctx.cfg)
    if str.length(ctx.port.sysconfdir) > 0
        sysconfdir = ctx.port.sysconfdir
    end if

    // PKG_ROOT is the alternate root (empty means /)
    let pkg_root = config.get_root(ctx.cfg)

    // Build the command with all environment variables
    // Per Hammerhead porting guide: no uname wrappers, replace bundled config.guess/config.sub,
    // use system config.guess for BUILD_TRIPLE (returns x86_64-zygaena-hammerhead)
    mut cmd = "cd \"" + ctx.work_dir + "\" && "
    cmd = cmd + "export SRCDIR=\"" + ctx.work_dir + "/$(" + find_srcdir_cmd + ")\" && "
    cmd = cmd + "export PKGDIR=\"" + destdir + "\" && "
    cmd = cmd + "export DESTDIR=\"" + destdir + "\" && "
    cmd = cmd + "export PREFIX=\"" + prefix + "\" && "
    cmd = cmd + "export SYSCONFDIR=\"" + sysconfdir + "\" && "
    cmd = cmd + "export PKG_ROOT=\"" + pkg_root + "\" && "
    cmd = cmd + "export JOBS=\"" + jobs_str + "\" && "
    cmd = cmd + "export CFLAGS=\"" + cflags + "\" && "
    cmd = cmd + "export CXXFLAGS=\"" + cxxflags + "\" && "
    cmd = cmd + "export LDFLAGS=\"-R" + prefix + "/lib\" && "
    cmd = cmd + "export PKG_CONFIG_PATH=\"" + prefix + "/lib/pkgconfig\" && "
    cmd = cmd + "export MAKEFLAGS=\"-j" + jobs_str + "\" && "
    cmd = cmd + "export PKG_NAME=\"" + ctx.port.info.name + "\" && "
    cmd = cmd + "export PKG_VERSION=\"" + ctx.port.info.version + "\" && "
    cmd = cmd + "export BUILD_TRIPLE=\"$(/usr/share/autoconf/build-aux/config.guess 2>/dev/null || echo x86_64-zygaena-hammerhead)\" && "
    cmd = cmd + "export PATH=\"/usr/gnu/bin:$PATH\" && "
    cmd = cmd + "export MAKE=gmake && "
    cmd = cmd + "export TAR=gtar && "
    cmd = cmd + "for _f in config.guess config.sub; do find \"$SRCDIR\" -name \"$_f\" -exec cp /usr/share/autoconf/build-aux/\"$_f\" {} \\; 2>/dev/null; done; "
    cmd = cmd + "zsh \"" + build_script + "\""

    log_info(ctx, "Running build script...")

    let exit_code = run_logged_cmd(ctx, cmd)
    if exit_code != 0
        log_error(ctx, "Build script failed with exit code: " + int_to_str(exit_code))
        return false
    end if

    log_success(ctx, "Build completed successfully")
    return true
end run_build

// Compress uncompressed man pages in the staging directory
proc compress_man_pages(ctx: BuildContext)
    // Find uncompressed man pages and gzip them
    // Uses -exec {} + (batch mode) to avoid shell escaping issues with \;
    // Uses -type f to skip symlinks (avoids creating dangling refs)
    let script = "find '" + ctx.pkg_dir + "' -type f -path '*/man/man*/*' ! -name '*.gz' ! -name '*.bz2' ! -name '*.xz' -exec gzip -9 {} +"

    let pid = process.process_spawn_shell(script)
    if pid > 0
        let exit_code = process.process_wait(pid)
        if exit_code == 0
            log_info(ctx, "Compressed man pages")
        end if
    end if
end compress_man_pages

fn create_package(ctx: BuildContext): bool
    color.print_action("Creating package...")

    // Validate that staging directory has content (not just metadata files)
    if not validate_staging_dir(ctx.pkg_dir)
        color.print_error("Staging directory is empty - build produced no files")
        color.print_error("Check that build.sh installs files to $DESTDIR or $PKGDIR")
        return false
    end if

    // Generate .PKGINFO
    if not generate_pkginfo(ctx)
        return false
    end if

    // Generate .MANIFEST (mtree file inventory)
    if not generate_manifest_file(ctx)
        return false
    end if

    // Create the package archive
    let pkg_name = ctx.port.info.name + "-" + ctx.port.info.version + "-" + int_to_str(ctx.port.info.release) + ".pkg.tar.xz"
    let pkg_path = path.join_path(config.get_packages_dir(ctx.cfg), pkg_name)

    if not archive.create_package(ctx.pkg_dir, pkg_path)
        color.print_error("Failed to create package archive")
        return false
    end if

    return true
end create_package

// Validate that staging directory has actual content (not empty)
fn validate_staging_dir(pkg_dir: string): bool
    // Count files in staging directory, excluding metadata files
    // A valid package should have at least one real file
    let cmd = "find \"" + pkg_dir + "\" -type f ! -name '.PKGINFO' ! -name '.MANIFEST' ! -name '.FOOTPRINT' | head -1"
    let tmp_file = "/tmp/coral_validate_" + int_to_str(process.getpid()) + ".txt"
    let full_cmd = cmd + " > \"" + tmp_file + "\""

    let pid = process.process_spawn_shell(full_cmd)
    if pid < 0
        return false
    end if

    process.process_wait(pid)

    // Read result
    let content = file.readFile(tmp_file)

    // Clean up
    let rm_cmd = "rm -f \"" + tmp_file + "\""
    let rm_pid = process.process_spawn_shell(rm_cmd)
    if rm_pid > 0
        process.process_wait(rm_pid)
    end if

    // If content is empty, staging dir has no real files
    if str.length(content) == 0
        return false
    end if

    // Trim newlines and check again
    let trimmed = str.trim(content, '\n')
    if str.length(trimmed) == 0
        return false
    end if

    return true
end validate_staging_dir

fn generate_pkginfo(ctx: BuildContext): bool
    let pkginfo_path = path.join_path(ctx.pkg_dir, ".PKGINFO")

    mut content = "[package]\n"
    content = content + "name = \"" + ctx.port.info.name + "\"\n"
    content = content + "version = \"" + ctx.port.info.version + "\"\n"
    content = content + "release = " + int_to_str(ctx.port.info.release) + "\n"
    content = content + "description = \"" + ctx.port.info.description + "\"\n"
    content = content + "url = \"" + ctx.port.info.url + "\"\n"
    content = content + "license = \"" + ctx.port.info.license + "\"\n"
    content = content + "maintainer = \"" + ctx.port.info.maintainer + "\"\n"
    content = content + "arch = \"" + ctx.port.info.arch + "\"\n"

    // Emit [dependencies] section if there are any
    let has_runtime = ctx.port.deps.runtime_count > 0
    let has_build = ctx.port.deps.build_count > 0

    if has_runtime or has_build
        content = content + "\n[dependencies]\n"

        if has_runtime
            content = content + "runtime = ["
            mut i = 0
            while i < ctx.port.deps.runtime_count
                if i > 0
                    content = content + ", "
                end if
                content = content + "\"" + ctx.port.deps.runtime[i] + "\""
                i = i + 1
            end while
            content = content + "]\n"
        end if

        if has_build
            content = content + "build = ["
            mut i = 0
            while i < ctx.port.deps.build_count
                if i > 0
                    content = content + ", "
                end if
                content = content + "\"" + ctx.port.deps.build[i] + "\""
                i = i + 1
            end while
            content = content + "]\n"
        end if
    end if

    return file.writeFile(pkginfo_path, content)
end generate_pkginfo

fn generate_manifest_file(ctx: BuildContext): bool
    let manifest_path = path.join_path(ctx.pkg_dir, ".MANIFEST")

    // Generate mtree manifest using native fs operations
    // BUG-033 (GC candidate overflow) fixed in Reef 0.5.6
    // uid/gid resolution now uses zero-allocation stat.uid_name()/gid_name()
    let content = mtree.generate_manifest(ctx.pkg_dir)
    if str.length(content) == 0
        return false
    end if

    return file.writeFile(manifest_path, content)
end generate_manifest_file

proc cleanup_build(ctx: BuildContext)
    // Check if cleanup is disabled by --noclean flag
    if ctx.noclean
        color.print_info("Skipping cleanup (--noclean specified)")
        return
    end if

    // Determine cleanup behavior from config and flags
    mut clean_work = config.get_clean_work(ctx.cfg)
    mut clean_pkg = config.get_clean_pkg(ctx.cfg)

    // --clean flag forces cleanup
    if ctx.force_clean
        clean_work = true
        clean_pkg = true
    end if

    // Clean work directory
    if clean_work and dir.dir_exists(ctx.work_dir)
        color.print_info("Cleaning work directory: " + ctx.work_dir)
        let cmd = "rm -rf \"" + ctx.work_dir + "\""
        let pid = process.process_spawn_shell(cmd)
        if pid > 0
            let exit_code = process.process_wait(pid)
            if exit_code == 0
                color.print_success("Cleaned work directory")
            else
                color.print_warning("Failed to clean work directory")
            end if
        end if
    end if

    // Clean pkg directory (DESTDIR staging)
    if clean_pkg and dir.dir_exists(ctx.pkg_dir)
        color.print_info("Cleaning pkg directory: " + ctx.pkg_dir)
        let cmd = "rm -rf \"" + ctx.pkg_dir + "\""
        let pid = process.process_spawn_shell(cmd)
        if pid > 0
            let exit_code = process.process_wait(pid)
            if exit_code == 0
                color.print_success("Cleaned pkg directory")
            else
                color.print_warning("Failed to clean pkg directory")
            end if
        end if
    end if
end cleanup_build

// Helper: extract filename from URL
fn extract_filename(url: string): string
    let last_slash = str.last_index_of_char(url, '/')
    if last_slash < 0
        return url
    end if

    let filename = str.substring(url, last_slash + 1, str.length(url) - last_slash - 1)

    // Remove query string if present
    let query_pos = str.index_of_char(filename, '?')
    if query_pos > 0
        return str.substring(filename, 0, query_pos)
    end if

    return filename
end extract_filename

// Helper: convert int to string
fn int_to_str(n: int): string
    if n == 0
        return "0"
    end if

    mut negative = false
    mut value = n
    if n < 0
        negative = true
        value = 0 - n
    end if

    mut result = ""
    while value > 0
        let digit = value % 10
        result = str.concat(str.substring("0123456789", digit, 1), result)
        value = value / 10
    end while

    if negative
        result = str.concat("-", result)
    end if

    return result
end int_to_str

end module