From fc5792f96973d3ca27887fe678cfbcdcd0d55699 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Mon, 16 May 2022 16:27:38 -0600 Subject: [PATCH] Build self-contained Convert-BpfToNative.ps1 (#1097) * Build self-contained Convert-BpfToNative.ps1 Signed-off-by: Alan Jowett * Update cmake build with code to generate Convert-BpfToNative.ps1 Signed-off-by: Alan Jowett Co-authored-by: Alan Jowett --- .github/workflows/cicd.yml | 1 + .github/workflows/reusable-build.yml | 15 +++++++ cmake/process_bpf_source_file.cmake | 2 +- scripts/Process-File.ps1 | 29 ++++++++++++ scripts/Replace-Text.ps1 | 10 +++++ tests/bpf2c_tests/expected/bindmonitor_dll.c | 6 +-- .../expected/bindmonitor_ringbuf_dll.c | 6 +-- .../expected/bindmonitor_tailcall_dll.c | 6 +-- tests/bpf2c_tests/expected/bpf_call_dll.c | 6 +-- tests/bpf2c_tests/expected/bpf_dll.c | 6 +-- .../expected/cgroup_sock_addr_dll.c | 6 +-- .../expected/decap_permit_packet_dll.c | 6 +-- .../bpf2c_tests/expected/divide_by_zero_dll.c | 6 +-- tests/bpf2c_tests/expected/droppacket_dll.c | 6 +-- .../expected/encap_reflect_packet_dll.c | 6 +-- tests/bpf2c_tests/expected/map_dll.c | 6 +-- tests/bpf2c_tests/expected/map_in_map_dll.c | 6 +-- .../bpf2c_tests/expected/map_in_map_v2_dll.c | 6 +-- tests/bpf2c_tests/expected/map_reuse_2_dll.c | 6 +-- tests/bpf2c_tests/expected/map_reuse_dll.c | 6 +-- tests/bpf2c_tests/expected/printk_dll.c | 6 +-- .../bpf2c_tests/expected/printk_legacy_dll.c | 6 +-- .../bpf2c_tests/expected/reflect_packet_dll.c | 6 +-- tests/bpf2c_tests/expected/sockops_dll.c | 6 +-- .../bpf2c_tests/expected/tail_call_bad_dll.c | 6 +-- tests/bpf2c_tests/expected/tail_call_dll.c | 6 +-- .../bpf2c_tests/expected/tail_call_map_dll.c | 6 +-- .../expected/tail_call_multiple_dll.c | 6 +-- .../expected/test_sample_ebpf_dll.c | 6 +-- .../expected/test_utility_helpers_dll.c | 6 +-- tests/sample/sample.vcxproj | 12 +++-- tools/bpf2c/CMakeLists.txt | 45 ++++++++++++++++++- .../bpf2c/Convert-BpfToNative.ps1.template | 29 +++++++----- tools/bpf2c/bpf2c.vcxproj | 10 ++++- tools/bpf2c/bpf2c_dll.c | 6 +-- tools/bpf2c/replacements.json | 23 ++++++++++ .../bpf2c/templates/kernel_mode_bpf2c.vcxproj | 6 +-- tools/bpf2c/templates/sources.def | 8 ---- tools/bpf2c/templates/user_mode_bpf2c.vcxproj | 22 ++++----- tools/nuget/ebpf-for-windows.nuspec | 5 ++- tools/nuget/ebpf-for-windows.props | 2 + 41 files changed, 201 insertions(+), 174 deletions(-) create mode 100644 scripts/Process-File.ps1 create mode 100644 scripts/Replace-Text.ps1 rename scripts/Convert-BpfToNative.ps1 => tools/bpf2c/Convert-BpfToNative.ps1.template (55%) create mode 100644 tools/bpf2c/replacements.json delete mode 100644 tools/bpf2c/templates/sources.def diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index b9c5f9662..71a36cbbf 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -40,6 +40,7 @@ jobs: with: build_artifact: Build-x64-vcxproj generate_release_package: true + build_nuget: true cmake: # Always run this job. diff --git a/.github/workflows/reusable-build.yml b/.github/workflows/reusable-build.yml index 38372a197..2f57424e5 100644 --- a/.github/workflows/reusable-build.yml +++ b/.github/workflows/reusable-build.yml @@ -22,6 +22,9 @@ on: build_codeql: required: false type: boolean + build_nuget: + required: false + type: boolean permissions: contents: read @@ -166,6 +169,18 @@ jobs: name: ebpf-for-windows.msi path: ${{ steps.packages.outputs.REL_MSI_PACKAGE_PATH }} + - name: Build nuget package + if: matrix.configurations == 'Release' && inputs.build_nuget == true + working-directory: ${{env.GITHUB_WORKSPACE}} + run: msbuild /m /p:Configuration=${{env.BUILD_CONFIGURATION}} /p:Platform=${{env.BUILD_PLATFORM}} ${{env.SOLUTION_FILE_PATH}} ${{env.BUILD_OPTIONS}} /t:tools\nuget + + - name: Upload the nuget package + if: matrix.configurations == 'Release' && inputs.build_nuget == true + uses: actions/upload-artifact@6673cd052c4cd6fcf4b4e6e60ea986c889389535 + with: + name: ebpf-for-windows nuget + path: ${{github.workspace}}/${{env.BUILD_PLATFORM}}/${{env.BUILD_CONFIGURATION}}/*.nupkg + - name: Perform CodeQL Analysis if: inputs.build_codeql == true uses: github/codeql-action/analyze@2f58583a1b24a7d3c7034f6bf9fa506d23b1183b diff --git a/cmake/process_bpf_source_file.cmake b/cmake/process_bpf_source_file.cmake index d1894427d..2e23a7b49 100644 --- a/cmake/process_bpf_source_file.cmake +++ b/cmake/process_bpf_source_file.cmake @@ -11,7 +11,7 @@ function(add_bpftonative_command file_name kernel_mode unsafe_program) set (output_file ${output_dir}/${file_name}_um.dll) endif() - set(command_line ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${CMAKE_SOURCE_DIR}/scripts/Convert-BpfToNative.ps1 -ProgramName ${file_name} -SolutionDir ${CMAKE_SOURCE_DIR} -OutputDir ${output_dir} -Platform x64 -Configuration $(Configuration) -KernelMode ${kernel_mode}) + set(command_line ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${output_dir}/Convert-BpfToNative.ps1 -IncludeDir ${CMAKE_SOURCE_DIR}/include -ProgramName ${file_name} -OutDir ${output_dir} -Platform x64 -Configuration $(Configuration) -KernelMode ${kernel_mode}) set (input_file ${output_dir}/${file_name}.o) if (${unsafe_program}) diff --git a/scripts/Process-File.ps1 b/scripts/Process-File.ps1 new file mode 100644 index 000000000..a662e9e1c --- /dev/null +++ b/scripts/Process-File.ps1 @@ -0,0 +1,29 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +# Process-File Reads $InputFile and $ConfigFile and performs replacement of strings in the content of $InputFile based +# on the Files key in the $ConfigFile. +# $ConfigFile is a json document containing the following elements: +# Array called "Files" that contains objects with properties "Symbol" and "Filename". +# Array called "EscapeCharacters" that contains objects with properties "Char" and "Escape". +# +# Each string with the value from "Symbol" is replaced with the contents from the corresponding "FileName" after +# each occurence of "Char" in the file is replaced with "Escape". + +param ([Parameter(Mandatory=$True)] [string] $InputFile, + [Parameter(Mandatory=$True)] [string] $OutputFile, + [Parameter(Mandatory=$True)] [string] $ConfigFile) + +$Config = Get-Content -Path $ConfigFile | ConvertFrom-Json +$Data = Get-Content -Path $InputFile + +$Config.Files | ForEach-Object { + $InsertString = Get-Content -Path $_.FileName + $Config.EscapeCharacters | ForEach-Object { + $InsertString = $InsertString.Replace($_.Char, $_.Escape) + } + + $Data = $Data.Replace($_.Symbol, $InsertString) +} + +Set-Content -Path $OutputFile -Value $Data \ No newline at end of file diff --git a/scripts/Replace-Text.ps1 b/scripts/Replace-Text.ps1 new file mode 100644 index 000000000..8f2559a43 --- /dev/null +++ b/scripts/Replace-Text.ps1 @@ -0,0 +1,10 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +param ($InputFile, $OutputFile, $TemplateText, $Replacement) + +$lines = Get-Content $InputFile + + + +$lines | Out-File $OutputFile \ No newline at end of file diff --git a/tests/bpf2c_tests/expected/bindmonitor_dll.c b/tests/bpf2c_tests/expected/bindmonitor_dll.c index 236c3e708..03773e68a 100644 --- a/tests/bpf2c_tests/expected/bindmonitor_dll.c +++ b/tests/bpf2c_tests/expected/bindmonitor_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/bindmonitor_ringbuf_dll.c b/tests/bpf2c_tests/expected/bindmonitor_ringbuf_dll.c index 9b626529d..677353584 100644 --- a/tests/bpf2c_tests/expected/bindmonitor_ringbuf_dll.c +++ b/tests/bpf2c_tests/expected/bindmonitor_ringbuf_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/bindmonitor_tailcall_dll.c b/tests/bpf2c_tests/expected/bindmonitor_tailcall_dll.c index a7ca662d9..e529d285f 100644 --- a/tests/bpf2c_tests/expected/bindmonitor_tailcall_dll.c +++ b/tests/bpf2c_tests/expected/bindmonitor_tailcall_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/bpf_call_dll.c b/tests/bpf2c_tests/expected/bpf_call_dll.c index 0d32734f7..cc5e639a5 100644 --- a/tests/bpf2c_tests/expected/bpf_call_dll.c +++ b/tests/bpf2c_tests/expected/bpf_call_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/bpf_dll.c b/tests/bpf2c_tests/expected/bpf_dll.c index 9a082f395..de54b1624 100644 --- a/tests/bpf2c_tests/expected/bpf_dll.c +++ b/tests/bpf2c_tests/expected/bpf_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/cgroup_sock_addr_dll.c b/tests/bpf2c_tests/expected/cgroup_sock_addr_dll.c index dc5dc7d12..57e61ecbf 100644 --- a/tests/bpf2c_tests/expected/cgroup_sock_addr_dll.c +++ b/tests/bpf2c_tests/expected/cgroup_sock_addr_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/decap_permit_packet_dll.c b/tests/bpf2c_tests/expected/decap_permit_packet_dll.c index 2a508aa64..7ac8372b8 100644 --- a/tests/bpf2c_tests/expected/decap_permit_packet_dll.c +++ b/tests/bpf2c_tests/expected/decap_permit_packet_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/divide_by_zero_dll.c b/tests/bpf2c_tests/expected/divide_by_zero_dll.c index 690eef6ec..f4ad1d1b7 100644 --- a/tests/bpf2c_tests/expected/divide_by_zero_dll.c +++ b/tests/bpf2c_tests/expected/divide_by_zero_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/droppacket_dll.c b/tests/bpf2c_tests/expected/droppacket_dll.c index 7f0bb301a..24fb86209 100644 --- a/tests/bpf2c_tests/expected/droppacket_dll.c +++ b/tests/bpf2c_tests/expected/droppacket_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/encap_reflect_packet_dll.c b/tests/bpf2c_tests/expected/encap_reflect_packet_dll.c index 3dd692467..95bfeb335 100644 --- a/tests/bpf2c_tests/expected/encap_reflect_packet_dll.c +++ b/tests/bpf2c_tests/expected/encap_reflect_packet_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/map_dll.c b/tests/bpf2c_tests/expected/map_dll.c index ff99fe52c..0d903d3ac 100644 --- a/tests/bpf2c_tests/expected/map_dll.c +++ b/tests/bpf2c_tests/expected/map_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/map_in_map_dll.c b/tests/bpf2c_tests/expected/map_in_map_dll.c index ae3a7338d..72c3c538e 100644 --- a/tests/bpf2c_tests/expected/map_in_map_dll.c +++ b/tests/bpf2c_tests/expected/map_in_map_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/map_in_map_v2_dll.c b/tests/bpf2c_tests/expected/map_in_map_v2_dll.c index 456780966..75c76ac02 100644 --- a/tests/bpf2c_tests/expected/map_in_map_v2_dll.c +++ b/tests/bpf2c_tests/expected/map_in_map_v2_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/map_reuse_2_dll.c b/tests/bpf2c_tests/expected/map_reuse_2_dll.c index b5fe5352b..3e95f2650 100644 --- a/tests/bpf2c_tests/expected/map_reuse_2_dll.c +++ b/tests/bpf2c_tests/expected/map_reuse_2_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/map_reuse_dll.c b/tests/bpf2c_tests/expected/map_reuse_dll.c index 749fdee13..8a321b546 100644 --- a/tests/bpf2c_tests/expected/map_reuse_dll.c +++ b/tests/bpf2c_tests/expected/map_reuse_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/printk_dll.c b/tests/bpf2c_tests/expected/printk_dll.c index be427fa1f..f7607dab4 100644 --- a/tests/bpf2c_tests/expected/printk_dll.c +++ b/tests/bpf2c_tests/expected/printk_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/printk_legacy_dll.c b/tests/bpf2c_tests/expected/printk_legacy_dll.c index 5c25b78c0..7576971d4 100644 --- a/tests/bpf2c_tests/expected/printk_legacy_dll.c +++ b/tests/bpf2c_tests/expected/printk_legacy_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/reflect_packet_dll.c b/tests/bpf2c_tests/expected/reflect_packet_dll.c index 641ad18ae..2c1fa0b1b 100644 --- a/tests/bpf2c_tests/expected/reflect_packet_dll.c +++ b/tests/bpf2c_tests/expected/reflect_packet_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/sockops_dll.c b/tests/bpf2c_tests/expected/sockops_dll.c index ee546966d..13e2ed632 100644 --- a/tests/bpf2c_tests/expected/sockops_dll.c +++ b/tests/bpf2c_tests/expected/sockops_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/tail_call_bad_dll.c b/tests/bpf2c_tests/expected/tail_call_bad_dll.c index 686878154..b6900a6c4 100644 --- a/tests/bpf2c_tests/expected/tail_call_bad_dll.c +++ b/tests/bpf2c_tests/expected/tail_call_bad_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/tail_call_dll.c b/tests/bpf2c_tests/expected/tail_call_dll.c index acf6a0d2f..14e9e277a 100644 --- a/tests/bpf2c_tests/expected/tail_call_dll.c +++ b/tests/bpf2c_tests/expected/tail_call_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/tail_call_map_dll.c b/tests/bpf2c_tests/expected/tail_call_map_dll.c index 5560a9a13..6cab837f2 100644 --- a/tests/bpf2c_tests/expected/tail_call_map_dll.c +++ b/tests/bpf2c_tests/expected/tail_call_map_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/tail_call_multiple_dll.c b/tests/bpf2c_tests/expected/tail_call_multiple_dll.c index 71f6432eb..d9e2c1a60 100644 --- a/tests/bpf2c_tests/expected/tail_call_multiple_dll.c +++ b/tests/bpf2c_tests/expected/tail_call_multiple_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/test_sample_ebpf_dll.c b/tests/bpf2c_tests/expected/test_sample_ebpf_dll.c index 440ad4c03..10f50cdbe 100644 --- a/tests/bpf2c_tests/expected/test_sample_ebpf_dll.c +++ b/tests/bpf2c_tests/expected/test_sample_ebpf_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/bpf2c_tests/expected/test_utility_helpers_dll.c b/tests/bpf2c_tests/expected/test_utility_helpers_dll.c index 2e0e0a4f5..97722691d 100644 --- a/tests/bpf2c_tests/expected/test_utility_helpers_dll.c +++ b/tests/bpf2c_tests/expected/test_utility_helpers_dll.c @@ -40,11 +40,7 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } #include "bpf2c.h" diff --git a/tests/sample/sample.vcxproj b/tests/sample/sample.vcxproj index 3a5d35f88..b428434fa 100644 --- a/tests/sample/sample.vcxproj +++ b/tests/sample/sample.vcxproj @@ -97,8 +97,10 @@ CppCode clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c %(Filename).c -o $(OutputPath)%(Filename).o - powershell -NonInteractive -ExecutionPolicy Unrestricted $(SolutionDir)scripts\Convert-BpfToNative.ps1 -ProgramName %(Filename) -SolutionDir $(SolutionDir) -OutputDir $(OutDir) -Platform $(Platform) -Configuration $(Configuration) -KernelMode $true - powershell -NonInteractive -ExecutionPolicy Unrestricted $(SolutionDir)scripts\Convert-BpfToNative.ps1 -ProgramName %(Filename) -SolutionDir $(SolutionDir) -OutputDir $(OutDir) -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false + pushd $(OutDir) + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $true + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false + popd $(OutputPath)%(Filename).o;$(OutputPath)%(Filename)_um.dll;$(OutputPath)%(Filename).sys @@ -110,8 +112,10 @@ clang $(ClangFlags) -c %(Filename).c -o $(OutputPath)%(Filename).o clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c unsafe\%(Filename).c -o $(OutputPath)%(Filename).o - powershell -NonInteractive -ExecutionPolicy Unrestricted $(SolutionDir)scripts\Convert-BpfToNative.ps1 -ProgramName %(Filename) -SolutionDir $(SolutionDir) -OutputDir $(OutDir) -Platform $(Platform) -Configuration $(Configuration) -KernelMode $true -SkipVerification $true - powershell -NonInteractive -ExecutionPolicy Unrestricted $(SolutionDir)scripts\Convert-BpfToNative.ps1 -ProgramName %(Filename) -SolutionDir $(SolutionDir) -OutputDir $(OutDir) -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true + pushd $(OutDir) + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $true -SkipVerification $true + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true + popd $(OutputPath)%(Filename).o diff --git a/tools/bpf2c/CMakeLists.txt b/tools/bpf2c/CMakeLists.txt index 68458461e..5bfc5bd04 100644 --- a/tools/bpf2c/CMakeLists.txt +++ b/tools/bpf2c/CMakeLists.txt @@ -65,6 +65,44 @@ function(generateTemplate source_file_name) ) endfunction() +function(processFile source_file_name config_file) + find_program(powershell_path "powershell" REQUIRED) + + set(input_file_name "${CMAKE_CURRENT_LIST_DIR}/${source_file_name}.ps1.template") + set(output_file_name "${output_dir}/${source_file_name}.ps1") + set(config_file_name "${CMAKE_CURRENT_LIST_DIR}/${config_file}") + + add_custom_command( + OUTPUT + "${output_file_name}" + + COMMAND + "${powershell_path}" powershell -NonInteractive -ExecutionPolicy Unrestricted "${CMAKE_SOURCE_DIR}/scripts/Process-File.ps1" -InputFile "${input_file_name}" -OutputFile "${output_file_name}" -ConfigFile "${config_file_name}" + + VERBATIM + + COMMENT + "ebpf-for-windows - Generating: ${source_file_name}.ps1" + + WORKING_DIRECTORY + "${CMAKE_CURRENT_LIST_DIR}" + + ) + + add_custom_target("${source_file_name}_builder" + DEPENDS "${output_file_name}" + ) + + add_library("${source_file_name}" INTERFACE) + target_include_directories("${source_file_name}" INTERFACE + "${CMAKE_CURRENT_BINARY_DIR}" + ) + + add_dependencies("${source_file_name}" + "${source_file_name}_builder" + ) +endfunction() + generateTemplate("bpf2c_driver") target_link_libraries("bpf2c" PRIVATE "bpf2c_driver" @@ -75,9 +113,14 @@ target_link_libraries("bpf2c" PRIVATE "bpf2c_dll" ) +processFile("Convert-BpfToNative" "replacements.json") +target_link_libraries("bpf2c" PRIVATE + "Convert-BpfToNative" +) + if(EBPFFORWINDOWS_ENABLE_INSTALL) install( - TARGETS "bpf2c" + TARGETS "bpf2c" "Convert-BpfToNative" DESTINATION "." ) endif() diff --git a/scripts/Convert-BpfToNative.ps1 b/tools/bpf2c/Convert-BpfToNative.ps1.template similarity index 55% rename from scripts/Convert-BpfToNative.ps1 rename to tools/bpf2c/Convert-BpfToNative.ps1.template index 8bde032b1..4921fa146 100644 --- a/scripts/Convert-BpfToNative.ps1 +++ b/tools/bpf2c/Convert-BpfToNative.ps1.template @@ -2,13 +2,19 @@ # SPDX-License-Identifier: MIT param([parameter(Mandatory = $true)] [string] $ProgramName, - [parameter(Mandatory = $false)] [string] $SolutionDir = $Pwd, - [parameter(Mandatory = $false)] [string] $OutputDir = $Pwd, + [parameter(Mandatory = $false)] [string] $IncludeDir = "$PSScriptRoot\..\include", + [parameter(Mandatory = $false)] [string] $BinDir = "$PSScriptRoot", + [parameter(Mandatory = $false)] [string] $OutDir = "$PWD", [parameter(Mandatory = $false)] [string] $Platform = "x64", [ValidateSet("Release", "Debug")][parameter(Mandatory = $false)] [string] $Configuration = "Release", [parameter(Mandatory = $false)] [bool] $SkipVerification = $false, [parameter(Mandatory = $false)] [bool] $KernelMode = $true) +Push-Location $OutDir + +$KernelModeProject = '___KERNEL_MODE_VCXPROJ___' +$UserModeProject = '___USER_MODE_VCXPROJ___' + # If program name ends with .o, remove the suffix if ($ProgramName.EndsWith(".o")) { $ProgramName = $ProgramName.Substring(0, $ProgramName.Length - 2) @@ -23,25 +29,28 @@ if ($null -eq (Get-Command 'msbuild.exe' -ErrorAction SilentlyContinue)) { throw "Unable to locate msbuild.exe. This command needs to run within a 'Developer Command Prompt'" } -$fileExists = Test-Path -Path ("$OutputDir\$ProgramName.o") +$fileExists = Test-Path -Path ("$ProgramName.o") if (!$fileExists) { - $errorString = "Can't find program file: " + "$OutputDir\$ProgramName.o" + $errorString = "Can't find program file: " + "$ProgramName.o" throw $errorString } -Push-Location $SolutionDir -$ProjectFile = "$SolutionDir\tools\bpf2c\templates\kernel_mode_bpf2c.vcxproj" -if (!$KernelMode) { - $ProjectFile = "$SolutionDir\tools\bpf2c\templates\user_mode_bpf2c.vcxproj" +if ($KernelMode) { + $ProjectFile = "$ProgramName.vcxproj" + Set-Content -Path $ProjectFile -Value $KernelModeProject +} +else { + $ProjectFile = "$ProgramName_um.vcxproj" + Set-Content -Path $ProjectFile -Value $UserModeProject } $AdditionalOptions = If ($SkipVerification) {"--no-verify"} Else {""} -msbuild /p:SolutionDir="$SolutionDir\" /p:OutDir="$OutputDir" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:ProgramName="$ProgramName" /p:AdditionalOptions="$AdditionalOptions" $ProjectFile +msbuild /p:BinDir="$BinDir\" /p:OutDir="$PWD\" /p:IncludeDir="$IncludeDir" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:ProgramName="$ProgramName" /p:AdditionalOptions="$AdditionalOptions" $ProjectFile if ($LASTEXITCODE -ne 0) { throw "Build failed for $ProgramName.o" } -Pop-Location \ No newline at end of file +Pop-Location diff --git a/tools/bpf2c/bpf2c.vcxproj b/tools/bpf2c/bpf2c.vcxproj index 37a8c57f5..c50983839 100644 --- a/tools/bpf2c/bpf2c.vcxproj +++ b/tools/bpf2c/bpf2c.vcxproj @@ -121,6 +121,9 @@ Console true + + powershell -NonInteractive -ExecutionPolicy Unrestricted $(SolutionDir)scripts\Process-File.ps1 -InputFile Convert-BpfToNative.ps1.template -OutputFile $(OutDir)Convert-BpfToNative.ps1 -ConfigFile replacements.json + @@ -135,6 +138,9 @@ true true + + powershell -NonInteractive -ExecutionPolicy Unrestricted $(SolutionDir)scripts\Process-File.ps1 -InputFile Convert-BpfToNative.ps1.template -OutputFile $(OutDir)Convert-BpfToNative.ps1 -ConfigFile replacements.json + @@ -190,13 +196,13 @@ - true + Document true - true + Document diff --git a/tools/bpf2c/bpf2c_dll.c b/tools/bpf2c/bpf2c_dll.c index 1b511bfb6..c45c578a6 100644 --- a/tools/bpf2c/bpf2c_dll.c +++ b/tools/bpf2c/bpf2c_dll.c @@ -37,8 +37,4 @@ division_by_zero(uint32_t address) if (std::string(NAME) == #X) \ return &X; -metadata_table_t* -get_metadata_table() -{ - return &metadata_table; -} +__declspec(dllexport) metadata_table_t* get_metadata_table() { return &metadata_table; } diff --git a/tools/bpf2c/replacements.json b/tools/bpf2c/replacements.json new file mode 100644 index 000000000..e71d278ac --- /dev/null +++ b/tools/bpf2c/replacements.json @@ -0,0 +1,23 @@ +{ + "Copyright Notice" : [ + "Copyright (c) Microsoft Corporation", + "SPDX-License-Identifier: MIT" + ], + "Description" : "This file controls the replacement of symbols with escaped strings", + "Files": [ + { + "Symbol": "___KERNEL_MODE_VCXPROJ___", + "FileName": "templates\\kernel_mode_bpf2c.vcxproj" + }, + { + "Symbol": "___USER_MODE_VCXPROJ___", + "FileName": "templates\\user_mode_bpf2c.vcxproj" + } + ], + "EscapeCharacters": [ + { + "Char": "'", + "Escape": "''" + } + ] +} \ No newline at end of file diff --git a/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj b/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj index 4a57aa405..31ac8900f 100644 --- a/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj +++ b/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj @@ -72,10 +72,10 @@ - $(OutDir)bpf2c --bpf $(OutDir)$(ProgramName).o --sys $(AdditionalOptions) >$(OutDir)$(ProgramName)_driver.c + $(BinDir)bpf2c --bpf $(ProgramName).o --sys $(AdditionalOptions) >$(ProgramName)_driver.c - $(SolutionDir)include;%(AdditionalIncludeDirectories) + $(IncludeDir);%(AdditionalIncludeDirectories) 4189;4245;%(DisableSpecificWarnings) @@ -89,7 +89,7 @@ - + diff --git a/tools/bpf2c/templates/sources.def b/tools/bpf2c/templates/sources.def deleted file mode 100644 index 06a5c2a6b..000000000 --- a/tools/bpf2c/templates/sources.def +++ /dev/null @@ -1,8 +0,0 @@ -; -; Copyright (c) Microsoft Corporation -; SPDX-License-Identifier: MIT -; - -LIBRARY -EXPORTS - get_metadata_table \ No newline at end of file diff --git a/tools/bpf2c/templates/user_mode_bpf2c.vcxproj b/tools/bpf2c/templates/user_mode_bpf2c.vcxproj index c83541002..ad21f7b34 100644 --- a/tools/bpf2c/templates/user_mode_bpf2c.vcxproj +++ b/tools/bpf2c/templates/user_mode_bpf2c.vcxproj @@ -23,7 +23,7 @@ - + 16.0 @@ -59,17 +59,16 @@ true NotUsing pch.h - $(SolutionDir)include;%(AdditionalIncludeDirectories) + $(IncludeDir);%(AdditionalIncludeDirectories) 4189;4245;%(DisableSpecificWarnings) Windows true false - $(SolutionDir)tools\bpf2c\templates\sources.def - $(OutDir)bpf2c.exe --bpf $(OutDir)$(ProgramName).o --dll $(AdditionalOptions) >$(OutDir)$(ProgramName)_dll.c + $(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) >$(ProgramName)_dll.c @@ -78,7 +77,7 @@ true NotUsing pch.h - $(SolutionDir)include;%(AdditionalIncludeDirectories) + $(IncludeDir);%(AdditionalIncludeDirectories) 4189;4245;%(DisableSpecificWarnings) @@ -87,10 +86,9 @@ true true false - $(SolutionDir)tools\bpf2c\templates\sources.def - $(OutDir)bpf2c.exe --bpf $(OutDir)$(ProgramName).o --dll $(AdditionalOptions) >$(OutDir)$(ProgramName)_dll.c + $(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) >$(ProgramName)_dll.c @@ -99,17 +97,16 @@ true NotUsing pch.h - $(SolutionDir)include;%(AdditionalIncludeDirectories) + $(IncludeDir);%(AdditionalIncludeDirectories) 4189;4245;%(DisableSpecificWarnings) Windows true false - $(SolutionDir)tools\bpf2c\templates\sources.def - $(OutDir)bpf2c.exe --bpf $(OutDir)$(ProgramName).o --dll $(AdditionalOptions) >$(OutDir)$(ProgramName)_dll.c + $(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) >$(ProgramName)_dll.c @@ -118,7 +115,7 @@ true NotUsing pch.h - $(SolutionDir)include;%(AdditionalIncludeDirectories) + $(IncludeDir);%(AdditionalIncludeDirectories) 4189;4245;%(DisableSpecificWarnings) @@ -127,10 +124,9 @@ true true false - $(SolutionDir)tools\bpf2c\templates\sources.def - $(OutDir)bpf2c.exe --bpf $(OutDir)$(ProgramName).o --dll $(AdditionalOptions) >$(OutDir)$(ProgramName)_dll.c + $(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) >$(ProgramName)_dll.c diff --git a/tools/nuget/ebpf-for-windows.nuspec b/tools/nuget/ebpf-for-windows.nuspec index 6726f2da9..acf2bad57 100644 --- a/tools/nuget/ebpf-for-windows.nuspec +++ b/tools/nuget/ebpf-for-windows.nuspec @@ -6,7 +6,7 @@ eBPF for Windows SDK eBPF-for-Windows - 0.1.3 + 0.1.4 eBPF for Windows Contributors eBPF for Windows Contributors false @@ -18,8 +18,9 @@ - + + diff --git a/tools/nuget/ebpf-for-windows.props b/tools/nuget/ebpf-for-windows.props index 93be5d6bb..288565a82 100644 --- a/tools/nuget/ebpf-for-windows.props +++ b/tools/nuget/ebpf-for-windows.props @@ -14,6 +14,8 @@ @(EbpfLibs) -I $(MSBuildThisFileDirectory)include -I $(MSBuildThisFileDirectory)include/uapi + $(MSBuildThisFileDirectory)bin + $(MSBuildThisFileDirectory)include