From 967a044637f1615c709cb723dc61118fcc08dcdb Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 14 Dec 2024 00:10:07 -0500 Subject: [PATCH] Got .lib and .dlls setup for the C11 library --- .vscode/settings.json | 3 +- base/dependencies/macros.hpp | 2 +- gen_c_library/c_library.cpp | 2 +- gen_c_library/gen_c_lib.c | 3 + scripts/build.ci.ps1 | 118 ++++++++++-------------- scripts/helpers/vendor_toolchain.ps1 | 91 ++++++++++++++++-- scripts/package_release.ps1 | 22 ++++- {gen_c_library => test/c_library}/gen.c | 3 +- 8 files changed, 157 insertions(+), 87 deletions(-) create mode 100644 gen_c_library/gen_c_lib.c rename {gen_c_library => test/c_library}/gen.c (78%) diff --git a/.vscode/settings.json b/.vscode/settings.json index f3a91f7..4effcec 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -56,7 +56,8 @@ "new": "cpp", "typeinfo": "cpp", "unordered_map": "cpp", - "xstddef": "cpp" + "xstddef": "cpp", + "gen_singleheader.h": "c" }, "C_Cpp.intelliSenseEngineFallback": "disabled", "mesonbuild.configureOnOpen": true, diff --git a/base/dependencies/macros.hpp b/base/dependencies/macros.hpp index 5b23cf4..78bbe30 100644 --- a/base/dependencies/macros.hpp +++ b/base/dependencies/macros.hpp @@ -7,7 +7,7 @@ #if GEN_COMPILER_MSVC #ifdef GEN_DYN_LINK - #ifdef GEN_EXPORTS + #ifdef GEN_DYN_EXPORT #define GEN_API __declspec(dllexport) #else #define GEN_API __declspec(dllimport) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 3bbba0e..5511481 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -641,7 +641,7 @@ do \ if ( array_token->Front == array_entry ) array_token->Front = next; - parser_types.append(array_entry); + parser_types.append(array_entry); continue_for = false; } break; diff --git a/gen_c_library/gen_c_lib.c b/gen_c_library/gen_c_lib.c new file mode 100644 index 0000000..80b1e43 --- /dev/null +++ b/gen_c_library/gen_c_lib.c @@ -0,0 +1,3 @@ +#define GEN_IMPLEMENTATION +#define GEN_DONT_ENFORCE_GEN_TIME_GUARD +#include "gen/gen_singleheader.h" diff --git a/scripts/build.ci.ps1 b/scripts/build.ci.ps1 index 114880e..c38bea2 100644 --- a/scripts/build.ci.ps1 +++ b/scripts/build.ci.ps1 @@ -18,12 +18,12 @@ Push-Location $path_root $vendor = $null $release = $null [bool] $verbose = $false -[bool] $shared = $false -[bool] $dyn_link = $false [bool] $base = $false [bool] $segmented = $false [bool] $singleheader = $false -[bool] $c_library = $false +[bool] $c_lib = $false +[bool] $c_lib_static = $false +[bool] $c_lib_dyn = $false [bool] $unreal = $false [bool] $test = $false @@ -37,12 +37,12 @@ if ( $args ) { $args | ForEach-Object { "verbose" { $verbose = $true } "release" { $release = $true } "debug" { $release = $false } - "shared" { $shared = $true } - "dyn_link" { $dyn_link = $true } "base" { $base = $true } "segmented" { $segmented = $true } "singleheader" { $singleheader = $true } - "c_library" { $c_library = $true } + "c_lib" { $c_lib = $true } + "c_lib_static" { $c_lib_static = $true } + "c_lib_dyn" { $c_lib_dyn = $true } "unreal" { $unreal = $true } "test" { $test = $true } } @@ -75,7 +75,9 @@ else { $cannot_build = $base -eq $false $cannot_build = $cannot_build -and $segmented -eq $false $cannot_build = $cannot_build -and $singleheader -eq $false -$cannot_build = $cannot_build -and $c_library -eq $false +$cannot_build = $cannot_build -and $c_lib -eq $false +$cannot_build = $cannot_build -and $c_lib_static -eq $false +$cannot_build = $cannot_build -and $c_lib_dyn -eq $false $cannot_build = $cannot_build -and $unreal -eq $false $cannot_build = $cannot_build -and $test -eq $false if ( $cannot_build ) { @@ -213,7 +215,7 @@ if ( $singleheader ) Pop-Location } -if ( $c_library ) +if ( $c_lib -or $c_lib_static -or $c_lib_dyn ) { $path_build = join-path $path_c_library build $path_gen = join-path $path_c_library gen @@ -249,34 +251,38 @@ if ( $c_library ) write-host "`nc_library generator completed in $($time_taken.TotalMilliseconds) ms" } Pop-Location +} - $includes = @( $path_c_library ) - $unit = join-path $path_c_library "gen.c" - $executable = join-path $path_build "gen_c_library_test.exe" +if ( $c_lib_static ) +{ + $includes = @( $path_c_library ) + $unit = join-path $path_c_library "gen_c_lib.c" + $path_lib = join-path $path_build "gencpp_c11.lib" - if ($vendor -eq "clang") { - $compiler_args += '-x' - $compiler_args += 'c' - $compiler_args += '-std=c11' - } elseif ($vendor -eq "msvc") { - $compiler_args += "/TC" # Compile as C - $compiler_args += "/Zc:__cplusplus" # Fix __cplusplus macro - $compiler_args += "/std:c11" - } + $compiler_args = @() + $compiler_args += $flag_all_c + $compiler_args += $flag_updated_cpp_macro + $compiler_args += $flag_c11 - $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable + $linker_args = @() + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $path_lib +} - Push-Location $path_c_library - if ( Test-Path( $executable ) ) { - write-host "`nRunning c_library test" - $time_taken = Measure-Command { & $executable - | ForEach-Object { - write-host `t $_ -ForegroundColor Green - } - } - write-host "`nc_library generator completed in $($time_taken.TotalMilliseconds) ms" - } - Pop-Location +if ( $c_lib_dyn ) +{ + $includes = @( $path_c_library ) + $unit = join-path $path_c_library "gen_c_lib.c" + $path_dll = join-path $path_build "gencpp_c11.dll" + + $compiler_args = @() + $compiler_args += $flag_all_c + $compiler_args += $flag_updated_cpp_macro + $compiler_args += $flag_c11 + $compiler_args += ( $flag_define + 'GEN_DYN_LINK' ) + $compiler_args += ( $flag_define + 'GEN_DYN_EXPORT' ) + + $linker_args = @() + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $path_dll } if ( $unreal ) @@ -319,47 +325,24 @@ if ( $unreal ) . $refactor_unreal } -# TODO(Ed): The unit testing needs a full rewrite -if ( $test -and $false ) +if ( $test ) { - $path_gen = join-path $path_test gen - $path_gen_build = join-path $path_gen build - $path_build = join-path $path_test build - $path_original = join-path $path_gen original - $path_components = join-path $path_original components - $path_dependencies = join-path $path_original dependencies - $path_helpers = join-path $path_original helpers + $path_test_c_lib = join-path $path_test c_library + $path_build = join-path $path_test_c_lib build if ( -not(Test-Path($path_build) )) { New-Item -ItemType Directory -Path $path_build } - if ( -not(Test-Path($path_gen) )) { - New-Item -ItemType Directory -Path $path_gen - } - if ( -not(Test-Path($path_gen_build) )) { - New-Item -ItemType Directory -Path $path_gen_build - } - if ( -not(test-path $path_original)) { - new-item -ItemType Directory -Path $path_original - } - if ( -not(test-path $path_components)) { - new-item -ItemType Directory -Path $path_components - } - if ( -not(test-path $path_dependencies)) { - new-item -ItemType Directory -Path $path_dependencies - } - if ( -not(test-path $path_helpers)) { - new-item -ItemType Directory -Path $path_helpers - } - $path_bootstrap = join-path $path_project gen - - $includes = @( $path_bootstrap ) - $unit = join-path $path_test "test.cpp" - $executable = join-path $path_build "test.exe" + $includes = @( $path_c_library ) + $unit = join-path $path_test_c_lib "gen.c" + $executable = join-path $path_build "gen_c_library_test.exe" $compiler_args = @() $compiler_args += ( $flag_define + 'GEN_TIME' ) + $compiler_args += $flag_all_c + $compiler_args += $flag_updated_cpp_macro + $compiler_args += $flag_c11 $linker_args = @( $flag_link_win_subsystem_console @@ -367,16 +350,15 @@ if ( $test -and $false ) $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable - Push-Location $path_test - Write-Host $path_test + Push-Location $path_test_c_lib if ( Test-Path( $executable ) ) { - write-host "`nRunning test generator" + write-host "`nRunning c_library test" $time_taken = Measure-Command { & $executable | ForEach-Object { write-host `t $_ -ForegroundColor Green } } - write-host "`nTest generator completed in $($time_taken.TotalMilliseconds) ms" + write-host "`nc_library generator completed in $($time_taken.TotalMilliseconds) ms" } Pop-Location } diff --git a/scripts/helpers/vendor_toolchain.ps1 b/scripts/helpers/vendor_toolchain.ps1 index e3202f0..30fa75d 100644 --- a/scripts/helpers/vendor_toolchain.ps1 +++ b/scripts/helpers/vendor_toolchain.ps1 @@ -22,6 +22,40 @@ if ( $dev ) { } } +# Add new function for running lib.exe +function run-archiver +{ + param( $archiver, $library, $lib_args ) + + write-host "`Creating library $library" + if ( $verbose ) { + write-host "Lib manager config:" + $lib_args | ForEach-Object { + write-host $_ -ForegroundColor Cyan + } + } + + $time_taken = Measure-Command { + & $archiver $lib_args 2>&1 | ForEach-Object { + $color = 'White' + switch ($_){ + { $_ -match "error" } { $color = 'Red' ; break } + { $_ -match "warning" } { $color = 'Yellow'; break } + } + write-host `t $_ -ForegroundColor $color + } + } + + if ( $LASTEXITCODE -eq 0 ) { + write-host "$library creation finished in $($time_taken.TotalMilliseconds) ms`n" + return $true + } + else { + write-host "Library creation failed for $library`n" -ForegroundColor Red + return $false + } +} + function run-compiler { param( $compiler, $unit, $compiler_args ) @@ -95,7 +129,8 @@ function run-linker if ( $vendor -match "clang" ) { # https://clang.llvm.org/docs/ClangCommandLineReference.html - $flag_all_c = '-x c' + $flag_all_c = @('-x', 'c') + $flag_c11 = '-std=c11' $flag_all_cpp = '-x c++' $flag_compile = '-c' $flag_color_diagnostics = '-fcolor-diagnostics' @@ -174,8 +209,8 @@ if ( $vendor -match "clang" ) $map = join-path $path_output (split-path $map -Leaf) # This allows dll reloads at runtime to work (jankily, use below if not interested) - $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" - # $pdb = $binary -replace '\.(exe|dll)$', ".pdb" + # $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" + $pdb = $binary -replace '\.(exe|dll)$', ".pdb" $compiler_args += @( $flag_no_color_diagnostics, @@ -253,8 +288,8 @@ if ( $vendor -match "clang" ) $map = join-path $path_output (split-path $map -Leaf) # This allows dll reloads at runtime to work (jankily, use below if not interested) - $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" - # $pdb = $binary -replace '\.(exe|dll)$', ".pdb" + # $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" + $pdb = $binary -replace '\.(exe|dll)$', ".pdb" $compiler_args += @( $flag_no_color_diagnostics, @@ -308,10 +343,22 @@ if ( $vendor -match "clang" ) $linker_args += $_ + '.lib' } + # Check if output is a static library + # if ( $binary -match '\.lib$' ) + # { + # $lib_args = @() + # $lib_args += $flag_nologo + # $lib_args += $flag_link_win_machine_64 + # $lib_args += ( $flag_link_win_path_output + $binary ) + # $lib_args += $object + # return run-archiver $archiver $binary $lib_args + # } + $linker_args += $object return run-linker $linker $binary $linker_args } + $archiver = 'llvm-ar' $compiler = 'clang++' $linker = 'lld-link' } @@ -320,6 +367,7 @@ if ( $vendor -match "msvc" ) { # https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=msvc-170 $flag_all_c = '/TC' + $flag_c11 = '/std:c11' $flag_all_cpp = '/TP' $flag_compile = '/c' $flag_debug = '/Zi' @@ -332,6 +380,7 @@ if ( $vendor -match "msvc" ) $flag_dll = '/LD' $flag_dll_debug = '/LDd' $flag_linker = '/link' + # $flag_link_lib = '/lib' $flag_link_dll = '/DLL' $flag_link_no_incremental = '/INCREMENTAL:NO' $flag_link_mapfile = '/MAP:' @@ -354,15 +403,17 @@ if ( $vendor -match "msvc" ) $flag_optimized_debug_forceinline = '/d2Obforceinline' $flag_optimized_debug = '/Zo' $flag_ - $flag_out_name = '/OUT:' + # $flag_out_name = '/OUT:' $flag_path_interm = '/Fo' $flag_path_debug = '/Fd' $flag_path_output = '/Fe' $flag_preprocess_conform = '/Zc:preprocessor' + $flag_updated_cpp_macro = "/Zc:__cplusplus" $flag_set_stack_size = '/F' $flag_syntax_only = '/Zs' $flag_wall = '/Wall' $flag_warnings_as_errors = '/WX' + $flag_lib_list = '/LIST' function build { @@ -374,8 +425,8 @@ if ( $vendor -match "msvc" ) $map = join-path $path_output (split-path $map -Leaf) # This allows dll reloads at runtime to work (jankily, use below if not interested) - $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" - # $pdb = $binary -replace '\.(exe|dll)$', ".pdb" + # $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" + $pdb = $binary -replace '\.(exe|dll)$', ".pdb" $compiler_args += @( $flag_nologo, @@ -461,8 +512,8 @@ if ( $vendor -match "msvc" ) $map = join-path $path_output (split-path $map -Leaf) # This allows dll reloads at runtime to work (jankily, use below if not interested) - $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" - # $pdb = $binary -replace '\.(exe|dll)$', ".pdb" + # $pdb = $binary -replace '\.(exe|dll)$', "_$(get-random).pdb" + $pdb = $binary -replace '\.(exe|dll)$', ".pdb" $compiler_args += @( $flag_nologo, @@ -508,6 +559,17 @@ if ( $vendor -match "msvc" ) return $false; } + # Check if output is a static library + if ( $binary -match '\.lib$' ) + { + $lib_args = @() + $lib_args += $flag_nologo + $lib_args += $flag_link_win_machine_64 + $lib_args += ( $flag_link_win_path_output + $binary ) + $lib_args += $object + return run-archiver $archiver $binary $lib_args + } + $linker_args += @( $flag_nologo, $flag_link_win_machine_64, @@ -522,10 +584,19 @@ if ( $vendor -match "msvc" ) else { } + # Check if output is a dynamic library + if ( $binary -match '\.dll$' ) { + $linker_args += $flag_link_dll + } $linker_args += $object + # Write-Host "link args:" + # $linker_args | ForEach-Object { + # Write-Host "`t$_" -ForegroundColor Yellow + # } return run-linker $linker $binary $linker_args } + $archiver = 'lib' $compiler = 'cl' $linker = 'link' } diff --git a/scripts/package_release.ps1 b/scripts/package_release.ps1 index fade38b..1becaeb 100644 --- a/scripts/package_release.ps1 +++ b/scripts/package_release.ps1 @@ -4,16 +4,14 @@ Import-Module $misc $build = Join-Path $PSScriptRoot 'build.ci.ps1' if ( $IsWindows ) { - & $build release msvc base segmented singleheader unreal c_library msvc debug -} -else { - & $build release clang base segmented singleheader unreal c_library msvc debug + & $build release msvc debug base segmented singleheader unreal c_lib c_lib_static c_lib_dyn } $path_root = Get-ScriptRepoRoot $path_docs = Join-Path $path_root docs $path_base = Join-Path $path_root base $path_c_library = Join-Path $path_root gen_c_library +$path_c_library_build = Join-Path $path_c_library build $path_c_library_gen = Join-Path $path_c_library gen $path_segmented = Join-Path $path_root gen_segmented $path_segmented_gen = Join-Path $path_segmented gen @@ -74,7 +72,7 @@ Remove-Item -Path $path_release_content -Recurse prep-ReleaseContent Copy-Item -Verbose -Path $path_c_library\Readme.md -Destination $path_release_content Copy-Item -Verbose -Path $path_c_library_gen\gen_singleheader.h -Destination $path_release_content\gen.h -Compress-Archive -Path $path_release_content\* -DestinationPath $path_release\gencpp_c11_singleheader.zip -Force +Compress-Archive -Path $path_release_content\* -DestinationPath $path_release\gencpp_c11_singleheader.zip -Force Remove-Item -Path $path_release_content -Recurse # C Library Segmented @@ -87,6 +85,20 @@ Copy-Item -Verbose -Path $path_c_library_gen\gen.h -Destination $path Compress-Archive -Path $path_release_content\* -DestinationPath $path_release\gencpp_c11_segmented.zip -Force Remove-Item -Path $path_release_content -Recurse +# C Library Segmented +prep-ReleaseContent +Copy-Item -Verbose -Path $path_c_library\Readme.md -Destination $path_release_content +Copy-Item -Verbose -Path $path_c_library_gen\gen.dep.c -Destination $path_release_content +Copy-Item -Verbose -Path $path_c_library_gen\gen.dep.h -Destination $path_release_content +Copy-Item -Verbose -Path $path_c_library_gen\gen.c -Destination $path_release_content +Copy-Item -Verbose -Path $path_c_library_gen\gen.h -Destination $path_release_content +Compress-Archive -Path $path_release_content\* -DestinationPath $path_release\gencpp_c11_segmented.zip -Force +Remove-Item -Path $path_release_content -Recurse + +# C Lib Static & Dyanmic Libs +Copy-Item -Verbose -Path $path_c_library_build\gencpp_c11.lib -Destination $path_release +Copy-Item -Verbose -Path $path_c_library_build\gencpp_c11.dll -Destination $path_release + # Base prep-ReleaseContent diff --git a/gen_c_library/gen.c b/test/c_library/gen.c similarity index 78% rename from gen_c_library/gen.c rename to test/c_library/gen.c index 3c50dea..30ff855 100644 --- a/gen_c_library/gen.c +++ b/test/c_library/gen.c @@ -1,8 +1,9 @@ #define GEN_IMPLEMENTATION #include "gen/gen_singleheader.h" -int main() +int gen_main() { // init(); __debugbreak(); + return 0; }