aboutsummaryrefslogtreecommitdiff
path: root/premake5.lua
blob: 45a6fc422be9f4cc21be3203da9b343fb05ee3b2 (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
output_dir = "%{cfg.buildcfg}-%{cfg.system}"

llvm_version = "22.1.0"
llvm_vendored_dir = "ext/llvm"

host_arm = os.hostarch() == "ARM64" or os.hostarch() == "arm64"
host_architecture = host_arm and "ARM64" or "x64"

function llvm_release_asset()
    local host = os.host()

    if host == "macosx" then
        return host_arm and ("LLVM-" .. llvm_version .. "-macOS-ARM64.tar.xz") or nil
    elseif host == "linux" then
        return "LLVM-" .. llvm_version .. (host_arm and "-Linux-ARM64" or "-Linux-X64") .. ".tar.xz"
    elseif host == "windows" then
        return "clang+llvm-" .. llvm_version ..
            (host_arm and "-aarch64-pc-windows-msvc" or "-x86_64-pc-windows-msvc") .. ".tar.xz"
    end

    return nil
end

function llvm_prefix()
    local function usable(candidate)
        if candidate == nil or candidate == "" then
            return nil
        end

        local prefix = path.getabsolute(
            (candidate:gsub("%s+$", "")):gsub("\\", "/"),
            _MAIN_SCRIPT_DIR
        )

        return os.isdir(prefix .. "/include/lldb") and prefix or nil
    end

    local function newest_of(pattern)
        local matches = os.matchdirs(pattern)
        table.sort(matches)

        for index = #matches, 1, -1 do
            local prefix = usable(matches[index])

            if prefix ~= nil then
                return prefix
            end
        end

        return nil
    end

    local prefix = usable(os.getenv("LLVM_PREFIX"))
        or usable(llvm_vendored_dir)
        or usable(os.outputof("llvm-config --prefix"))

    if prefix == nil and os.host() == "windows" then
        prefix = usable(
            os.getenv("ProgramFiles") and
            os.getenv("ProgramFiles") .. "/LLVM"
        ) or newest_of("C:/Program Files/LLVM*")
    end

    if prefix == nil and os.host() == "macosx" then
        prefix = usable(os.outputof("brew --prefix llvm"))
    end

    if prefix == nil and os.host() == "linux" then
        prefix = newest_of("/usr/lib/llvm-*")
            or newest_of("/usr/local/llvm*")
    end

    return prefix
end

llvm_dir = llvm_prefix()

if llvm_dir == nil
    and _ACTION ~= nil
    and _ACTION ~= "fetch-llvm"
    and _ACTION ~= "clean"
    and _ACTION ~= "check"
then
    error(
        "no llvm with lldb headers found. install one and set LLVM_PREFIX, " ..
        "or run `premake5 fetch-llvm` to download the pinned " ..
        llvm_version .. " release"
    )
end

llvm_dir = llvm_dir or ""

include_dir = {
    glfw = "ext/glfw/include",
    glad = "ext/glad/include",
    imgui = "ext/imgui",
    imgui_backends = "ext/imgui/backends",
    lldb = llvm_dir .. "/include",
}

library_dir = {
    lldb = llvm_dir .. "/lib",
}

function lldb_library()
    if llvm_dir == "" then
        return ""
    end

    local names = { "liblldb.so" }

    if os.target() == "windows" then
        names = { "liblldb.lib" }
    elseif os.target() == "macosx" then
        names = { "liblldb.dylib" }
    end

    for _, name in ipairs(names) do
        local candidate = library_dir.lldb .. "/" .. name

        if os.isfile(candidate) then
            return candidate
        end
    end

    local versioned = os.matchfiles(
        library_dir.lldb .. "/liblldb.so.*"
    )

    table.sort(versioned)

    if versioned[#versioned] ~= nil then
        return versioned[#versioned]
    end

    if os.target() ~= os.host() then
        return library_dir.lldb .. "/" .. names[1]
    end

    error(
        "llvm at " .. llvm_dir ..
        " has lldb headers but no liblldb in " ..
        library_dir.lldb
    )
end

function windows_runtime_copies()
    local commands = {}

    if llvm_dir == "" then
        return commands
    end

    local runtimes = os.matchfiles(
        llvm_dir .. "/bin/liblldb.dll"
    )

    for _, runtime in ipairs(
        os.matchfiles(llvm_dir .. "/bin/python*.dll")
    ) do
        table.insert(runtimes, runtime)
    end

    if #runtimes == 0 then
        runtimes = {
            llvm_dir .. "/bin/liblldb.dll",
        }
    end

    for _, runtime in ipairs(runtimes) do
        table.insert(
            commands,
            '{COPYFILE} "' .. runtime .. '" "%{cfg.targetdir}"'
        )
    end

    return commands
end

function setup_target()
    targetdir("bin/" .. output_dir)
    objdir("bin-int/" .. output_dir .. "/%{prj.name}")

    staticruntime "On"

    filter "system:windows"
        staticruntime "Off"
    filter {}
end

function setup_c_target()
    setup_target()

    filter { "system:linux or system:macosx" }
        buildoptions {
            "-Wall",
            "-Wextra",
            "-Wpedantic",
            "-Wconversion",
            "-Wsign-conversion",
            "-Wshadow",
            "-Wstrict-prototypes",
            "-Wmissing-prototypes",
        }
    filter {}

    filter "configurations:debug"
        runtime "Debug"
        symbols "On"

        filter { "system:linux or system:macosx" }
            buildoptions {
                "-g",
                "-fno-omit-frame-pointer",
                "-fsanitize=address,undefined",
            }

            linkoptions {
                "-fsanitize=address,undefined",
            }
        filter {}

    filter { "configurations:release or configurations:dist" }
        runtime "Release"
        optimize "Speed"
    filter {}

    filter "configurations:dist"
        symbols "Off"
    filter {}
end

function setup_cpp_target()
    setup_target()

    cppdialect "C++23"

    filter { "system:linux or system:macosx" }
        buildoptions {
            "-Wall",
            "-Wextra",
            "-Wpedantic",
            "-Wconversion",
            "-Wsign-conversion",
            "-Wshadow",
        }
    filter {}

    filter "configurations:debug"
        runtime "Debug"
        symbols "On"

        filter { "system:linux or system:macosx" }
            buildoptions {
                "-g",
                "-fno-omit-frame-pointer",
                "-fsanitize=address,undefined",
            }

            linkoptions {
                "-fsanitize=address,undefined",
            }
        filter {}

    filter { "configurations:release or configurations:dist" }
        runtime "Release"
        optimize "Speed"
    filter {}

    filter "configurations:dist"
        symbols "Off"
    filter {}
end

newaction {
    trigger = "clean",
    description = "Remove build output and generated project files",

    execute = function()
        os.rmdir("bin")
        os.rmdir("bin-int")
        os.rmdir(".vs")
        os.remove("Makefile")
        os.remove("imgui.ini")
        os.remove("compile_commands.json")

        local patterns = {
            "*.make",
            "*.sln",
            "*.vcxproj",
            "*.vcxproj.*",
        }

        for _, pattern in ipairs(patterns) do
            for _, file in ipairs(os.matchfiles(pattern)) do
                os.remove(file)
            end
        end

        print("Cleaned build output and generated project files")
    end
}

newaction {
    trigger = "check",
    description = "Run static analysis with cppcheck",

    execute = function()
        -- clean the same configuration we are about to build, otherwise its
        -- objects stay up to date, the build below compiles nothing, and bear
        -- captures an empty compile_commands.json
        local result = os.execute("make clean config=dist")

        if result ~= true and result ~= 0 then
            error("Failed to clean project")
        end

        -- analyze the dist configuration: it drops the debug-only asserts and
        -- sanitizers, so cppcheck sees the code as it actually ships and catches
        -- issues the debug build hides (e.g. variables only read by an assert)
        result = os.execute("bear -- make config=dist")

        if result ~= true and result ~= 0 then
            error("Failed to generate compile_commands.json")
        end

        -- only our own code is in scope: skip the vendored translation units and
        -- drop any finding that lands in a third-party header our sources include
        result = os.execute(
            "cppcheck " ..
            "--project=compile_commands.json " ..
            "-i ext " ..
            "--suppress='*:ext/*' " ..
            "--inline-suppr " ..
            "--enable=warning,style,performance,portability " ..
            "--error-exitcode=1"
        )

        if result ~= true and result ~= 0 then
            error("cppcheck found issues")
        end
    end
}

newaction {
    trigger = "fetch-llvm",
    description = "Download the pinned llvm release into " .. llvm_vendored_dir,

    execute = function()
        if os.isdir(llvm_vendored_dir .. "/include/lldb") then
            print(
                llvm_vendored_dir ..
                " already has an llvm with lldb headers"
            )
            return
        end

        local asset = llvm_release_asset()

        if asset == nil then
            error(
                "no pinned llvm release for " ..
                os.host() .. " " .. os.hostarch() ..
                ", install llvm yourself and set LLVM_PREFIX"
            )
        end

        local archive = "ext/" .. asset
        local staging = "ext/llvm-staging"

        if not os.isfile(archive) then
            local url =
                "https://github.com/llvm/llvm-project/releases/download/llvmorg-" ..
                llvm_version .. "/" .. asset

            print("downloading " .. url)

            local reported = 0

            local result, code = http.download(
                url,
                archive,
                {
                    progress = function(total, current)
                        if total > 0
                            and current - reported >= total / 20
                        then
                            reported = current

                            printf(
                                "  %d%% (%.0f mb)",
                                math.floor(current / total * 100),
                                current / 1048576
                            )
                        end
                    end
                }
            )

            if result ~= "OK" then
                os.remove(archive)

                error(
                    "download failed with " ..
                    tostring(code) .. ": " ..
                    tostring(result)
                )
            end
        end

        os.rmdir(staging)
        os.mkdir(staging)

        print("extracting " .. asset)

        local extracted = os.execute(
            'tar -xf "' .. archive ..
            '" -C "' .. staging .. '"'
        )

        if extracted ~= true and extracted ~= 0 then
            error(
                "could not extract " .. archive ..
                ", the system tar may lack xz support, " ..
                "unpack it into " .. llvm_vendored_dir ..
                " by hand"
            )
        end

        local unpacked = os.matchdirs(staging .. "/*")[1]

        if unpacked == nil then
            error(archive .. " did not contain an llvm directory")
        end

        os.rmdir(llvm_vendored_dir)

        if not os.rename(unpacked, llvm_vendored_dir) then
            error(
                "could not move " .. unpacked ..
                " to " .. llvm_vendored_dir
            )
        end

        os.rmdir(staging)
        os.remove(archive)

        print(
            "llvm " .. llvm_version ..
            " is ready in " .. llvm_vendored_dir
        )
    end
}

workspace "hsdbg"
    architecture(host_architecture)
    startproject "hsdbg"
    multiprocessorcompile "On"

    configurations {
        "debug",
        "release",
        "dist",
    }

    filter "system:windows"
        defines "HSDBG_WINDOWS"

    filter "system:linux"
        defines "HSDBG_LINUX"

    filter "system:macosx"
        defines "HSDBG_MACOS"

    filter {}

group "dependencies"

project "glfw"
    kind "StaticLib"
    language "C"
    warnings "Off"

    setup_c_target()

    files {
        "ext/glfw/src/context.c",
        "ext/glfw/src/init.c",
        "ext/glfw/src/input.c",
        "ext/glfw/src/monitor.c",
        "ext/glfw/src/platform.c",
        "ext/glfw/src/vulkan.c",
        "ext/glfw/src/window.c",

        "ext/glfw/src/internal.h",
        "ext/glfw/src/platform.h",
        "ext/glfw/src/mappings.h",

        "ext/glfw/src/null_init.c",
        "ext/glfw/src/null_joystick.c",
        "ext/glfw/src/null_joystick.h",
        "ext/glfw/src/null_monitor.c",
        "ext/glfw/src/null_platform.h",
        "ext/glfw/src/null_window.c",
    }

    filter "system:linux"
        pic "On"
        defines "_GLFW_X11"

        files {
            "ext/glfw/src/x11_init.c",
            "ext/glfw/src/x11_monitor.c",
            "ext/glfw/src/x11_platform.h",
            "ext/glfw/src/x11_window.c",
            "ext/glfw/src/xkb_unicode.c",
            "ext/glfw/src/xkb_unicode.h",

            "ext/glfw/src/wl_init.c",
            "ext/glfw/src/wl_monitor.c",
            "ext/glfw/src/wl_platform.h",
            "ext/glfw/src/wl_window.c",

            "ext/glfw/src/posix_module.c",
            "ext/glfw/src/posix_time.c",
            "ext/glfw/src/posix_time.h",
            "ext/glfw/src/posix_thread.c",
            "ext/glfw/src/posix_thread.h",
            "ext/glfw/src/posix_poll.c",
            "ext/glfw/src/posix_poll.h",

            "ext/glfw/src/glx_context.c",
            "ext/glfw/src/egl_context.c",
            "ext/glfw/src/osmesa_context.c",

            "ext/glfw/src/linux_joystick.c",
            "ext/glfw/src/linux_joystick.h",
        }

    filter "system:macosx"
        pic "On"
        defines "_GLFW_COCOA"

        files {
            "ext/glfw/src/cocoa_init.m",
            "ext/glfw/src/cocoa_joystick.h",
            "ext/glfw/src/cocoa_joystick.m",
            "ext/glfw/src/cocoa_monitor.m",
            "ext/glfw/src/cocoa_platform.h",
            "ext/glfw/src/macos_time.c",
            "ext/glfw/src/macos_time.h",
            "ext/glfw/src/cocoa_window.m",

            "ext/glfw/src/nsgl_context.m",
            "ext/glfw/src/egl_context.c",
            "ext/glfw/src/osmesa_context.c",

            "ext/glfw/src/posix_module.c",
            "ext/glfw/src/posix_thread.c",
            "ext/glfw/src/posix_thread.h",
        }

    filter "system:windows"
        defines {
            "_GLFW_WIN32",
            "_CRT_SECURE_NO_WARNINGS",
        }

        files {
            "ext/glfw/src/win32_init.c",
            "ext/glfw/src/win32_joystick.c",
            "ext/glfw/src/win32_joystick.h",
            "ext/glfw/src/win32_module.c",
            "ext/glfw/src/win32_monitor.c",
            "ext/glfw/src/win32_platform.h",
            "ext/glfw/src/win32_thread.c",
            "ext/glfw/src/win32_thread.h",
            "ext/glfw/src/win32_time.c",
            "ext/glfw/src/win32_time.h",
            "ext/glfw/src/win32_window.c",
            "ext/glfw/src/wgl_context.c",
            "ext/glfw/src/egl_context.c",
            "ext/glfw/src/osmesa_context.c",
        }

    filter {}

project "glad"
    kind "StaticLib"
    language "C"
    warnings "Off"

    setup_c_target()

    files {
        "ext/glad/include/**.h",
        "ext/glad/src/**.c",
    }

    includedirs "%{include_dir.glad}"

    filter { "system:linux or system:macosx" }
        pic "On"
    filter {}

project "imgui"
    kind "StaticLib"
    language "C++"
    warnings "Off"

    setup_cpp_target()

    defines "GLFW_INCLUDE_NONE"

    files {
        "ext/imgui/imconfig.h",
        "ext/imgui/imgui.h",
        "ext/imgui/imgui.cpp",
        "ext/imgui/imgui_draw.cpp",
        "ext/imgui/imgui_internal.h",
        "ext/imgui/imgui_tables.cpp",
        "ext/imgui/imgui_widgets.cpp",
        "ext/imgui/imgui_demo.cpp",
        "ext/imgui/imstb_rectpack.h",
        "ext/imgui/imstb_textedit.h",
        "ext/imgui/imstb_truetype.h",

        "ext/imgui/misc/cpp/imgui_stdlib.h",
        "ext/imgui/misc/cpp/imgui_stdlib.cpp",

        "ext/imgui/backends/imgui_impl_glfw.h",
        "ext/imgui/backends/imgui_impl_glfw.cpp",
        "ext/imgui/backends/imgui_impl_opengl3.h",
        "ext/imgui/backends/imgui_impl_opengl3.cpp",
        "ext/imgui/backends/imgui_impl_opengl3_loader.h",
    }

    includedirs {
        "%{include_dir.imgui}",
        "%{include_dir.imgui_backends}",
        "%{include_dir.glfw}",
    }

    filter { "system:linux or system:macosx" }
        pic "On"

    filter "system:macosx"
        defines "GL_SILENCE_DEPRECATION"

    filter "system:windows"
        defines "_CRT_SECURE_NO_WARNINGS"

    filter {}

group ""

project "hsdbg"
    kind "ConsoleApp"
    language "C++"

    setup_cpp_target()

    defines {
        "_CRT_SECURE_NO_WARNINGS",
        "GLFW_INCLUDE_NONE",
    }

    files {
        "src/**.h",
        "src/**.cpp",
    }

    includedirs {
        "src",
        "%{include_dir.glfw}",
        "%{include_dir.glad}",
        "%{include_dir.imgui}",
        "%{include_dir.imgui_backends}",
        "%{include_dir.lldb}",
        "ext/stb",
    }

    links {
        "glfw",
        "glad",
        "imgui",
    }

    defines {
        "HSDBG_LLVM_PREFIX=\"" .. llvm_dir .. "\"",
        "HSDBG_ASSET_DIR=\"" .. _MAIN_SCRIPT_DIR .. "/assets\"",
    }

    linkoptions {
        lldb_library(),
    }

    filter "system:windows"
        systemversion "latest"

        links "opengl32"

        postbuildcommands(windows_runtime_copies())

    filter "system:not windows"
        linkoptions {
            "-Wl,-rpath," .. library_dir.lldb,
        }

    filter "system:linux"
        links {
            "GL",
            "dl",
            "m",
            "pthread",
        }

    filter "system:macosx"
        defines "GL_SILENCE_DEPRECATION"

        links {
            "Cocoa.framework",
            "IOKit.framework",
            "CoreFoundation.framework",
            "CoreVideo.framework",
            "OpenGL.framework",
            "QuartzCore.framework",
        }

    filter "configurations:debug"
        defines "HSDBG_DEBUG"

    filter "configurations:release"
        defines "HSDBG_RELEASE"

    filter "configurations:dist"
        defines "HSDBG_DIST"

    filter {}