From 9565529c08a3f04a33d13307c6c2b7f082213432 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 24 Nov 2026 00:02:42 +0000 Subject: [PATCH 1/3] BUILD: Build the native libraries with every warning on and as an error The Device Detection and IP Intelligence native builds include the cxx CMake files, so these settings cover the cxx libraries as well as the SWIG wrapper. Every target warns at /W4 with MSVC and at -Wall with gcc and clang, the level cxx builds its own tests with, and every warning is an error through CMAKE_COMPILE_WARNING_AS_ERROR. The wrapper's link warnings are errors too. MSVC warnings 5105 and 4100 are no longer turned off, the wrapper no longer defines _CRT_SECURE_NO_WARNINGS, and it is built with the SWIG macros that keep warning 4505 and the CRT and C++ library deprecation warnings on. CMake 3.24 or later is needed, as cxx already needs. PreBuild.bat stops at the first CMake command that fails. The helpers in hash_java.i that add evidence and look up a property by name read each byte array up to its first zero or its length, whichever comes first, so the lengths SWIG passes are used. Java_Hash_Engine.cpp and Java_Ipi_Engine.cpp are SWIG's output for the changed files. ci/native-warnings.Tests.ps1 checks these settings, and the CI script tests now run on a change to the native build files. --- .github/workflows/ci-script-tests.yml | 5 +- ci/native-warnings.Tests.ps1 | 116 ++++++++++++++++++ .../src/main/cxx/CMakeLists.txt | 23 ++-- .../src/main/cxx/Java_Hash_Engine.cpp | 23 ++-- .../src/main/cxx/PreBuild.bat | 8 +- .../src/main/cxx/hash_java.i | 25 ++-- .../src/main/cxx/CMakeLists.txt | 23 ++-- .../src/main/cxx/Java_Ipi_Engine.cpp | 23 ++-- .../src/main/cxx/PreBuild.bat | 4 +- .../src/main/cxx/hash_java.i | 25 ++-- 10 files changed, 226 insertions(+), 49 deletions(-) create mode 100644 ci/native-warnings.Tests.ps1 diff --git a/.github/workflows/ci-script-tests.yml b/.github/workflows/ci-script-tests.yml index de3989ad9..749430040 100644 --- a/.github/workflows/ci-script-tests.yml +++ b/.github/workflows/ci-script-tests.yml @@ -4,7 +4,8 @@ name: CI script tests # builds run them. The environment variable names workflow runs its own test. # The test of run-fresh-clone-example.ps1 also runs the calls in # examples-fresh-clone.yml, and workflow-keys.Tests.ps1 reads every workflow, -# so a change to any workflow runs the tests too. +# so a change to any workflow runs the tests too. native-warnings.Tests.ps1 +# reads the native build files, so a change to those runs them as well. on: pull_request: @@ -14,6 +15,7 @@ on: # run-fresh-clone-example.Tests.ps1 runs the calls examples-fresh-clone.yml # makes, and workflow-keys.Tests.ps1 checks the keys every workflow reads. - '.github/workflows/**' + - '**/src/main/cxx/**' push: branches: - main @@ -21,6 +23,7 @@ on: paths: - 'ci/**' - '.github/workflows/**' + - '**/src/main/cxx/**' workflow_dispatch: permissions: diff --git a/ci/native-warnings.Tests.ps1 b/ci/native-warnings.Tests.ps1 new file mode 100644 index 000000000..2fdd0218e --- /dev/null +++ b/ci/native-warnings.Tests.ps1 @@ -0,0 +1,116 @@ +# Pester 5 tests of the warnings the two native engines build with. Every C and +# C++ warning is an error on every compiler, at the level the cxx submodule +# builds its own tests with, and nothing in these files turns a warning off. + +BeforeDiscovery { + $Engines = @( + @{ + Engine = 'Device Detection' + Folder = 'device-detection/device-detection.hash.engine.on-premise/src/main/cxx' + Wrapper = 'Java_Hash_Engine.cpp' + } + @{ + Engine = 'IP Intelligence' + Folder = 'ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx' + Wrapper = 'Java_Ipi_Engine.cpp' + } + ) +} + +BeforeAll { + $Root = Join-Path $PSScriptRoot '..' + + # The lines of a file without comments. Comments start with # in CMake and + # with rem or :: in a batch file. + function Get-Code([string]$Path) { + @(Get-Content (Join-Path $Root $Path) | ForEach-Object { + if ($Path -like '*.bat') { + if ($_ -notmatch '^\s*(rem\b|::)') { $_ } + } else { + $_ -replace '#.*$', '' + } + }) + } + + # The CMake lines before the file includes cxx, which apply to every target + # cxx adds as well as to the wrapper. + function Get-LinesBeforeCxx([string]$Path) { + foreach ($line in Get-Code $Path) { + if ($line -match '^\s*include\(.*cxx/CMakeLists\.txt') { + break + } + $line + } + } + + # Switches that hide warnings from MSVC, GCC or Clang, or that turn off the + # CRT and C++ library deprecation warnings. + $Hiding = @( + '(? engine native build' -ForEach $Engines { + BeforeAll { + $CMake = "$Folder/CMakeLists.txt" + } + + # PreBuild.bat is left out because its own /w argument names the Windows + # SDK. It passes the compiler nothing. + It 'turns no warning off in <_>' -ForEach @( + 'CMakeLists.txt', 'PreBuild.sh', 'hash_java.i') { + $code = (Get-Code "$Folder/$_") -join "`n" + foreach ($pattern in $Hiding) { + $code | Should -Not -MatchExactly $pattern + } + } + + It 'needs a CMake that leaves the warning level out of the default flags' { + $code = (Get-Code $CMake) -join "`n" + $code -match 'cmake_minimum_required\(VERSION\s+(?\d+\.\d+)[\s)]' | + Should -BeTrue + [version]$Matches['v'] | Should -BeGreaterOrEqual ([version]'3.24') + } + + It 'warns at /W4 and -Wall on every target' { + $before = (Get-LinesBeforeCxx $CMake) -join "`n" + $before | Should -MatchExactly 'add_compile_options\([^)]*/W4' + $before | Should -MatchExactly 'add_compile_options\([^)]*-Wall' + } + + It 'makes every warning an error' { + $before = (Get-LinesBeforeCxx $CMake) -join "`n" + $before | Should -Match 'set\(\s*CMAKE_COMPILE_WARNING_AS_ERROR\s+ON\s*\)' + (Get-Code $CMake) -join "`n" | + Should -MatchExactly 'target_link_options\([^)]*/WX' + } + + It 'keeps the warnings the SWIG wrapper would turn off' { + $code = (Get-Code $CMake) -join "`n" + foreach ($name in 'SWIG_MSC_UNSUPPRESS_4505', + 'SWIG_NO_CRT_SECURE_NO_DEPRECATE', 'SWIG_NO_SCL_SECURE_NO_DEPRECATE') { + $code | Should -Match "target_compile_definitions\([^)]*\b$name\b" + } + } + + It 'stops PreBuild.bat at the first CMake command that fails' { + $commands = @(Get-Code "$Folder/PreBuild.bat" | + Where-Object { $_ -match '^\s*cmake\s' }) + $commands.Count | Should -BeGreaterThan 0 + foreach ($command in $commands) { + $command | Should -Match '\|\|\s*exit\s+/b\s+1\s*$' + } + } + + It 'stops PreBuild.sh at the first command that fails' { + (Get-Code "$Folder/PreBuild.sh") -join "`n" | Should -Match '(?m)^\s*set\s+-e' + } +} diff --git a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt index 36b35dca0..b55fab196 100644 --- a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt +++ b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt @@ -1,16 +1,19 @@ -cmake_minimum_required(VERSION 3.0...3.10 FATAL_ERROR) +# 3.24 or later, as cxx needs. From 3.15 CMake puts no warning level in the +# default MSVC flags, so the level set below is the only one. +cmake_minimum_required(VERSION 3.24) set (CMAKE_C_STANDARD 11) set (CMAKE_CXX_STANDARD 11) # Include the C API project(DeviceDetectionHashEngine VERSION 4.3.0 LANGUAGES CXX C) +# Every target here, the wrapper and the cxx libraries alike, warns at the +# level cxx builds its own tests with, and every warning is an error. +set(CMAKE_COMPILE_WARNING_AS_ERROR ON) if (MSVC) - add_compile_options(/wd5105 /wd4100) + add_compile_options(/W4) else() - add_compile_options(-fPIC) -# if (NOT Apple) -# endif() + add_compile_options(-Wall -fPIC) endif() if (MSVC) @@ -106,10 +109,16 @@ if (NOT MSVC) target_compile_options(fiftyone-hash-java INTERFACE "-static-libgcc -static-libstdc++") endif() +# The SWIG wrapper turns some MSVC warnings off unless these are defined. +target_compile_definitions(fiftyone-hash-java PRIVATE + SWIG_MSC_UNSUPPRESS_4505 + SWIG_NO_CRT_SECURE_NO_DEPRECATE + SWIG_NO_SCL_SECURE_NO_DEPRECATE) if (MSVC) - target_compile_options(fiftyone-hash-java PRIVATE "/D_CRT_SECURE_NO_WARNINGS" "/W4" "/WX") + target_compile_options(fiftyone-hash-java PRIVATE "/W4" "/WX") + target_link_options(fiftyone-hash-java PRIVATE "/WX") else() - target_compile_options(fiftyone-hash-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Werror") + target_compile_options(fiftyone-hash-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Werror") endif() # The wrapper includes through cxx/, so the folder that holds it is on the path. diff --git a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/Java_Hash_Engine.cpp b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/Java_Hash_Engine.cpp index c9fd06e97..482f53005 100644 --- a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/Java_Hash_Engine.cpp +++ b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/Java_Hash_Engine.cpp @@ -610,27 +610,36 @@ SWIGINTERN void std_vector_Sl_uint32_t_Sg__doRemoveRange(std::vector< uint32_t > } } + static std::string fiftyoneBytesToString(const char bytes[], size_t length) { + size_t used = 0; + while (used < length && bytes[used] != '\0') { + used++; + } + return used == 0 ? std::string() : std::string(bytes, used); + } + + void Evidence_AddFromBytes(EvidenceBase *evidence, const char key[], size_t keyLength, const char value[], size_t valueLength) { - (*evidence)[key] = value; + (*evidence)[fiftyoneBytesToString(key, keyLength)] = fiftyoneBytesToString(value, valueLength); } Value Results_GetValueAsString(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsString(propertyName); + return results->getValueAsString(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value> Results_GetValues(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValues(propertyName); + return results->getValues(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsBool(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsBool(propertyName); + return results->getValueAsBool(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsInteger(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsInteger(propertyName); + return results->getValueAsInteger(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsDouble(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsDouble(propertyName); + return results->getValueAsDouble(fiftyoneBytesToString(propertyName, propertyNameLength)); } bool Results_ContainsProperty(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->containsProperty(propertyName); + return results->containsProperty(fiftyoneBytesToString(propertyName, propertyNameLength)); } diff --git a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/PreBuild.bat b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/PreBuild.bat index 5b934f064..00fe1598c 100644 --- a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/PreBuild.bat +++ b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/PreBuild.bat @@ -61,10 +61,10 @@ if exist target\build64 rmdir /S /Q target\build64 mkdir target\build32 cd target\build32 -cmake ..\..\src\main\cxx -A Win32 %additionalFlags% -cmake --build . --target fiftyone-hash-java --config Release +cmake ..\..\src\main\cxx -A Win32 %additionalFlags% || exit /b 1 +cmake --build . --target fiftyone-hash-java --config Release || exit /b 1 cd ..\.. mkdir target\build64 cd target\build64 -cmake ..\..\src\main\cxx -A x64 %additionalFlags% -cmake --build . --target fiftyone-hash-java --config Release \ No newline at end of file +cmake ..\..\src\main\cxx -A x64 %additionalFlags% || exit /b 1 +cmake --build . --target fiftyone-hash-java --config Release || exit /b 1 \ No newline at end of file diff --git a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/hash_java.i b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/hash_java.i index 119ea2e22..bf330d4ab 100644 --- a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/hash_java.i +++ b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/hash_java.i @@ -38,28 +38,39 @@ nofinalize(ResultsHash); %apply (const char *STRING, size_t LENGTH) { (const char propertyName[], size_t propertyNameLength) } %apply (const char *STRING, size_t LENGTH) { (const char key[], size_t keyLength) } %apply (const char *STRING, size_t LENGTH) { (const char value[], size_t valueLength) } +/* The Java side ends each byte array with a zero, which the string leaves + out. The length stops the read at the end of the array if there is none. */ +%{ + static std::string fiftyoneBytesToString(const char bytes[], size_t length) { + size_t used = 0; + while (used < length && bytes[used] != '\0') { + used++; + } + return used == 0 ? std::string() : std::string(bytes, used); + } +%} %inline %{ void Evidence_AddFromBytes(EvidenceBase *evidence, const char key[], size_t keyLength, const char value[], size_t valueLength) { - (*evidence)[key] = value; + (*evidence)[fiftyoneBytesToString(key, keyLength)] = fiftyoneBytesToString(value, valueLength); } Value Results_GetValueAsString(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsString(propertyName); + return results->getValueAsString(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value> Results_GetValues(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValues(propertyName); + return results->getValues(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsBool(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsBool(propertyName); + return results->getValueAsBool(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsInteger(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsInteger(propertyName); + return results->getValueAsInteger(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsDouble(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsDouble(propertyName); + return results->getValueAsDouble(fiftyoneBytesToString(propertyName, propertyNameLength)); } bool Results_ContainsProperty(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->containsProperty(propertyName); + return results->containsProperty(fiftyoneBytesToString(propertyName, propertyNameLength)); } %} diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt index 9f6cdef95..140c11498 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt @@ -1,16 +1,19 @@ -cmake_minimum_required(VERSION 3.0...3.10 FATAL_ERROR) +# 3.24 or later, as cxx needs. From 3.15 CMake puts no warning level in the +# default MSVC flags, so the level set below is the only one. +cmake_minimum_required(VERSION 3.24) set (CMAKE_C_STANDARD 11) set (CMAKE_CXX_STANDARD 11) # Include the C API project(IPIntelligenceOnPremiseEngine VERSION 4.3.0 LANGUAGES CXX C) +# Every target here, the wrapper and the cxx libraries alike, warns at the +# level cxx builds its own tests with, and every warning is an error. +set(CMAKE_COMPILE_WARNING_AS_ERROR ON) if (MSVC) - add_compile_options(/wd5105 /wd4100) + add_compile_options(/W4) else() - add_compile_options(-fPIC) -# if (NOT Apple) -# endif() + add_compile_options(-Wall -fPIC) endif() if (MSVC) @@ -96,10 +99,16 @@ if (NOT MSVC) target_compile_options(fiftyone-ip-intelligence-java INTERFACE "-static-libgcc -static-libstdc++") endif() +# The SWIG wrapper turns some MSVC warnings off unless these are defined. +target_compile_definitions(fiftyone-ip-intelligence-java PRIVATE + SWIG_MSC_UNSUPPRESS_4505 + SWIG_NO_CRT_SECURE_NO_DEPRECATE + SWIG_NO_SCL_SECURE_NO_DEPRECATE) if (MSVC) - target_compile_options(fiftyone-ip-intelligence-java PRIVATE "/D_CRT_SECURE_NO_WARNINGS" "/W4" "/WX") + target_compile_options(fiftyone-ip-intelligence-java PRIVATE "/W4" "/WX") + target_link_options(fiftyone-ip-intelligence-java PRIVATE "/WX") else() - target_compile_options(fiftyone-ip-intelligence-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Werror") + target_compile_options(fiftyone-ip-intelligence-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Werror") endif() # The wrapper includes through cxx/, so the folder that holds it is on the path. diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/Java_Ipi_Engine.cpp b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/Java_Ipi_Engine.cpp index 1d586b7be..8ed304eae 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/Java_Ipi_Engine.cpp +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/Java_Ipi_Engine.cpp @@ -913,27 +913,36 @@ SWIGINTERN int ResultsIpi_copyValueAsUTF8String(ResultsIpi *self,int requiredPro return required; } + static std::string fiftyoneBytesToString(const char bytes[], size_t length) { + size_t used = 0; + while (used < length && bytes[used] != '\0') { + used++; + } + return used == 0 ? std::string() : std::string(bytes, used); + } + + void Evidence_AddFromBytes(EvidenceBase *evidence, const char key[], size_t keyLength, const char value[], size_t valueLength) { - (*evidence)[key] = value; + (*evidence)[fiftyoneBytesToString(key, keyLength)] = fiftyoneBytesToString(value, valueLength); } Value Results_GetValueAsString(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsString(propertyName); + return results->getValueAsString(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value> Results_GetValues(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValues(propertyName); + return results->getValues(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsBool(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsBool(propertyName); + return results->getValueAsBool(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsInteger(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsInteger(propertyName); + return results->getValueAsInteger(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsDouble(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsDouble(propertyName); + return results->getValueAsDouble(fiftyoneBytesToString(propertyName, propertyNameLength)); } bool Results_ContainsProperty(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->containsProperty(propertyName); + return results->containsProperty(fiftyoneBytesToString(propertyName, propertyNameLength)); } diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/PreBuild.bat b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/PreBuild.bat index 23b20dce4..4906871e3 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/PreBuild.bat +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/PreBuild.bat @@ -60,5 +60,5 @@ if exist target\build64 rmdir /S /Q target\build64 mkdir target\build64 cd target\build64 -cmake ..\..\src\main\cxx -A x64 %additionalFlags% -cmake --build . --target fiftyone-ip-intelligence-java --config Release \ No newline at end of file +cmake ..\..\src\main\cxx -A x64 %additionalFlags% || exit /b 1 +cmake --build . --target fiftyone-ip-intelligence-java --config Release || exit /b 1 \ No newline at end of file diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/hash_java.i b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/hash_java.i index c520c04d8..106b6471a 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/hash_java.i +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/hash_java.i @@ -32,28 +32,39 @@ nofinalize(ResultsIpi); %apply (char *STRING, size_t LENGTH) { (const char propertyName[], size_t propertyNameLength) } %apply (char *STRING, size_t LENGTH) { (const char key[], size_t keyLength) } %apply (char *STRING, size_t LENGTH) { (const char value[], size_t valueLength) } +/* The Java side ends each byte array with a zero, which the string leaves + out. The length stops the read at the end of the array if there is none. */ +%{ + static std::string fiftyoneBytesToString(const char bytes[], size_t length) { + size_t used = 0; + while (used < length && bytes[used] != '\0') { + used++; + } + return used == 0 ? std::string() : std::string(bytes, used); + } +%} %inline %{ void Evidence_AddFromBytes(EvidenceBase *evidence, const char key[], size_t keyLength, const char value[], size_t valueLength) { - (*evidence)[key] = value; + (*evidence)[fiftyoneBytesToString(key, keyLength)] = fiftyoneBytesToString(value, valueLength); } Value Results_GetValueAsString(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsString(propertyName); + return results->getValueAsString(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value> Results_GetValues(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValues(propertyName); + return results->getValues(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsBool(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsBool(propertyName); + return results->getValueAsBool(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsInteger(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsInteger(propertyName); + return results->getValueAsInteger(fiftyoneBytesToString(propertyName, propertyNameLength)); } Value Results_GetValueAsDouble(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->getValueAsDouble(propertyName); + return results->getValueAsDouble(fiftyoneBytesToString(propertyName, propertyNameLength)); } bool Results_ContainsProperty(ResultsBase *results, const char propertyName[], size_t propertyNameLength) { - return results->containsProperty(propertyName); + return results->containsProperty(fiftyoneBytesToString(propertyName, propertyNameLength)); } %} From 2465ea0ec09bb862d3d47f3b3c40ecae8bdb8d6b Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 24 Nov 2026 03:04:20 +0000 Subject: [PATCH 2/3] BUILD: Add -Wextra and SWIG's warnings, and check the cxx sources gcc and clang now build every target with -Wall -Wextra, and the SWIG wrapper with -Wall -Wextra -Werror, the level the cxx libraries build at. Both CMake SWIG runs and RegenSwig.ps1 pass -Wall -Werror, and the CMake configure stops when SWIG fails. Both engines ask for C++17, which the included cxx files already set. ci/native-warnings.Tests.ps1 also reads RegenSwig.ps1, checks that each generated wrapper turns no warning off outside SWIG's own guards, and checks the -Wextra and SWIG options. It reads every source and CMake file under the cxx submodule too, and ci-script-tests.yml checks out the submodule for that and runs when it moves. That check fails at the current submodule commit and passes once those sources turn no warning off. --- .github/workflows/ci-script-tests.yml | 10 +- ci/native-warnings.Tests.ps1 | 138 ++++++++++++++++-- .../src/main/cxx/CMakeLists.txt | 13 +- .../src/main/cxx/CMakeLists.txt | 13 +- .../src/main/cxx/RegenSwig.ps1 | 2 + 5 files changed, 151 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci-script-tests.yml b/.github/workflows/ci-script-tests.yml index 749430040..2dd01c9e1 100644 --- a/.github/workflows/ci-script-tests.yml +++ b/.github/workflows/ci-script-tests.yml @@ -5,7 +5,8 @@ name: CI script tests # The test of run-fresh-clone-example.ps1 also runs the calls in # examples-fresh-clone.yml, and workflow-keys.Tests.ps1 reads every workflow, # so a change to any workflow runs the tests too. native-warnings.Tests.ps1 -# reads the native build files, so a change to those runs them as well. +# reads the native build files and the cxx sources they compile, so a change +# to those, or a move of the cxx submodule, runs them as well. on: pull_request: @@ -16,6 +17,7 @@ on: # makes, and workflow-keys.Tests.ps1 checks the keys every workflow reads. - '.github/workflows/**' - '**/src/main/cxx/**' + - 'cxx' push: branches: - main @@ -24,6 +26,7 @@ on: - 'ci/**' - '.github/workflows/**' - '**/src/main/cxx/**' + - 'cxx' workflow_dispatch: permissions: @@ -43,6 +46,11 @@ jobs: with: persist-credentials: false + # native-warnings.Tests.ps1 reads the cxx sources. Their data files + # are not needed. + - name: Check out the cxx submodule + run: git submodule update --init --depth 1 cxx + - name: Run the tests shell: pwsh run: | diff --git a/ci/native-warnings.Tests.ps1 b/ci/native-warnings.Tests.ps1 index 2fdd0218e..3e8490c18 100644 --- a/ci/native-warnings.Tests.ps1 +++ b/ci/native-warnings.Tests.ps1 @@ -1,6 +1,7 @@ # Pester 5 tests of the warnings the two native engines build with. Every C and -# C++ warning is an error on every compiler, at the level the cxx submodule -# builds its own tests with, and nothing in these files turns a warning off. +# C++ warning is on and is an error on every compiler, every SWIG warning is an +# error, and nothing in these files, in the wrappers SWIG made or in the cxx +# sources the engines compile turns a warning off. BeforeDiscovery { $Engines = @( @@ -8,30 +9,113 @@ BeforeDiscovery { Engine = 'Device Detection' Folder = 'device-detection/device-detection.hash.engine.on-premise/src/main/cxx' Wrapper = 'Java_Hash_Engine.cpp' + Files = @('CMakeLists.txt', 'PreBuild.sh', 'hash_java.i') } @{ Engine = 'IP Intelligence' Folder = 'ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx' Wrapper = 'Java_Ipi_Engine.cpp' + Files = @('CMakeLists.txt', 'PreBuild.sh', 'hash_java.i', 'RegenSwig.ps1') } ) + + # The cxx sources and CMake files the engines include. Its tests and + # examples are not built here. + $Top = Join-Path $PSScriptRoot '..' + $CxxFiles = @( + Get-ChildItem -File -Recurse (Join-Path $Top 'cxx/src') | + Where-Object { $_.Extension -in '.c', '.h', '.cpp', '.hpp', '.i' -or + $_.Name -eq 'CMakeLists.txt' } | + ForEach-Object { + [IO.Path]::GetRelativePath($Top, $_.FullName).Replace('\', '/') + } + 'cxx/CMakeLists.txt' + ) } BeforeAll { $Root = Join-Path $PSScriptRoot '..' - # The lines of a file without comments. Comments start with # in CMake and - # with rem or :: in a batch file. + # The lines of a file without comments. Comments start with # in CMake, + # PowerShell and shell scripts, and with rem or :: in a batch file. Other + # files, such as SWIG and C files, are taken whole, as # starts a + # preprocessor line there. function Get-Code([string]$Path) { @(Get-Content (Join-Path $Root $Path) | ForEach-Object { if ($Path -like '*.bat') { if ($_ -notmatch '^\s*(rem\b|::)') { $_ } - } else { + } elseif ($Path -match '(CMakeLists\.txt|\.cmake|\.ps1|\.sh)$') { $_ -replace '#.*$', '' + } else { + $_ } }) } + # Each line of a file that turns a warning off, as path:line: text. + function Find-Hiding([string]$Path) { + $number = 0 + foreach ($line in Get-Code $Path) { + $number++ + foreach ($pattern in $Hiding) { + if ($line -cmatch $pattern) { + "${Path}:${number}: $($line.Trim())" + break + } + } + } + } + + # The lines of a SWIG wrapper that turn a warning off outside the guards + # SWIG puts them in, which the build keeps closed. The definitions + # SWIG_MSC_UNSUPPRESS_4505, SWIG_NO_CRT_SECURE_NO_DEPRECATE and + # SWIG_NO_SCL_SECURE_NO_DEPRECATE close the first three, and no release is + # built with the Intel compiler. + function Find-UnguardedHiding([string]$Path) { + $guards = @( + '^#\s*ifndef\s+SWIG_MSC_UNSUPPRESS_4505\b' + '^#\s*if\b.*!defined\(SWIG_NO_CRT_SECURE_NO_DEPRECATE\)' + '^#\s*if\b.*!defined\(SWIG_NO_SCL_SECURE_NO_DEPRECATE\)' + '^#\s*ifdef\s+__INTEL_COMPILER\b' + ) + $wrapperHiding = $Hiding + @( + 'pragma\s+warning\s+disable' + '#\s*define\s+_(CRT|SCL)_SECURE_NO_(DEPRECATE|WARNINGS)\b' + ) + # Whether each open #if block is a guard, innermost last. + $open = [System.Collections.Generic.List[bool]]::new() + $number = 0 + foreach ($line in Get-Content (Join-Path $Root $Path)) { + $number++ + $text = $line.Trim() + if ($text -match '^#\s*if') { + $guarded = $false + foreach ($guard in $guards) { + if ($text -match $guard) { $guarded = $true } + } + $open.Add($guarded) + continue + } + if ($text -match '^#\s*(else|elif)\b' -and $open.Count -gt 0) { + $open[$open.Count - 1] = $false + continue + } + if ($text -match '^#\s*endif\b' -and $open.Count -gt 0) { + $open.RemoveAt($open.Count - 1) + continue + } + if ($open -contains $true) { + continue + } + foreach ($pattern in $wrapperHiding) { + if ($text -cmatch $pattern) { + "${Path}:${number}: $text" + break + } + } + } + } + # The CMake lines before the file includes cxx, which apply to every target # cxx adds as well as to the wrapper. function Get-LinesBeforeCxx([string]$Path) { @@ -65,12 +149,12 @@ Describe 'the engine native build' -ForEach $Engines { # PreBuild.bat is left out because its own /w argument names the Windows # SDK. It passes the compiler nothing. - It 'turns no warning off in <_>' -ForEach @( - 'CMakeLists.txt', 'PreBuild.sh', 'hash_java.i') { - $code = (Get-Code "$Folder/$_") -join "`n" - foreach ($pattern in $Hiding) { - $code | Should -Not -MatchExactly $pattern - } + It 'turns no warning off in <_>' -ForEach $Files { + @(Find-Hiding "$Folder/$_") | Should -BeNullOrEmpty + } + + It 'turns no warning off in outside the guards the build keeps closed' { + @(Find-UnguardedHiding "$Folder/$Wrapper") | Should -BeNullOrEmpty } It 'needs a CMake that leaves the warning level out of the default flags' { @@ -80,10 +164,34 @@ Describe 'the engine native build' -ForEach $Engines { [version]$Matches['v'] | Should -BeGreaterOrEqual ([version]'3.24') } - It 'warns at /W4 and -Wall on every target' { + It 'warns at /W4 and -Wall -Wextra on every target' { $before = (Get-LinesBeforeCxx $CMake) -join "`n" $before | Should -MatchExactly 'add_compile_options\([^)]*/W4' $before | Should -MatchExactly 'add_compile_options\([^)]*-Wall' + $before | Should -MatchExactly 'add_compile_options\([^)]*-Wextra' + } + + It 'warns at /W4 and -Wall -Wextra on the wrapper' { + $code = (Get-Code $CMake) -join "`n" + foreach ($option in '/W4', '-Wall', '-Wextra') { + $code | Should -MatchExactly ( + 'target_compile_options\(\S+-java\s+PRIVATE[^)]*"' + + [regex]::Escape($option) + '"') + } + } + + It 'runs SWIG with every warning on, as an error that stops the build' { + $calls = @(Get-Code $CMake | Where-Object { $_ -match '\$\{SWIG_EXECUTABLE\}' }) + $calls.Count | Should -BeGreaterThan 0 + foreach ($call in $calls) { + $call | Should -MatchExactly '\$\{SWIG_EXECUTABLE\}\s+-Wall\s+-Werror\s' + $call | Should -MatchExactly 'COMMAND_ERROR_IS_FATAL\s+ANY' + } + # The scripts that run SWIG by hand pass the same options. + foreach ($script in $Files | Where-Object { $_ -like '*.ps1' }) { + (Get-Code "$Folder/$script") -join "`n" | + Should -MatchExactly '&\s*\$swigExec[\s`]+-Wall\s+-Werror\s' + } } It 'makes every warning an error' { @@ -114,3 +222,9 @@ Describe 'the engine native build' -ForEach $Engines { (Get-Code "$Folder/PreBuild.sh") -join "`n" | Should -Match '(?m)^\s*set\s+-e' } } + +Describe 'the cxx sources the native engines compile' { + It 'turns no warning off in <_>' -ForEach $CxxFiles { + @(Find-Hiding $_) | Should -BeNullOrEmpty + } +} diff --git a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt index b55fab196..093ef29ae 100644 --- a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt +++ b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt @@ -2,18 +2,19 @@ # default MSVC flags, so the level set below is the only one. cmake_minimum_required(VERSION 3.24) set (CMAKE_C_STANDARD 11) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 17) # Include the C API project(DeviceDetectionHashEngine VERSION 4.3.0 LANGUAGES CXX C) -# Every target here, the wrapper and the cxx libraries alike, warns at the -# level cxx builds its own tests with, and every warning is an error. +# Every target here, the wrapper and the cxx libraries alike, warns at /W4 +# with MSVC and at -Wall -Wextra with gcc and clang, and every warning is an +# error. set(CMAKE_COMPILE_WARNING_AS_ERROR ON) if (MSVC) add_compile_options(/W4) else() - add_compile_options(-Wall -fPIC) + add_compile_options(-Wall -Wextra -fPIC) endif() if (MSVC) @@ -80,7 +81,7 @@ if (RebuildSwig) message(WARNING "SWIG does not include Java support so will not be run") else() message(STATUS "Rebuilding SWIG interfaces") - execute_process(COMMAND ${SWIG_EXECUTABLE} -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.devicedetection.hash.engine.onpremise.interop.swig -outdir ${SRCOUT}/interop/swig -o ${CMAKE_CURRENT_LIST_DIR}/Java_Hash_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i) + execute_process(COMMAND ${SWIG_EXECUTABLE} -Wall -Werror -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.devicedetection.hash.engine.onpremise.interop.swig -outdir ${SRCOUT}/interop/swig -o ${CMAKE_CURRENT_LIST_DIR}/Java_Hash_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i COMMAND_ERROR_IS_FATAL ANY) endif() endif() message(STATUS "Done SWIG interfaces") @@ -118,7 +119,7 @@ if (MSVC) target_compile_options(fiftyone-hash-java PRIVATE "/W4" "/WX") target_link_options(fiftyone-hash-java PRIVATE "/WX") else() - target_compile_options(fiftyone-hash-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Werror") + target_compile_options(fiftyone-hash-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Wextra" "-Werror") endif() # The wrapper includes through cxx/, so the folder that holds it is on the path. diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt index 140c11498..bc6343a02 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt @@ -2,18 +2,19 @@ # default MSVC flags, so the level set below is the only one. cmake_minimum_required(VERSION 3.24) set (CMAKE_C_STANDARD 11) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 17) # Include the C API project(IPIntelligenceOnPremiseEngine VERSION 4.3.0 LANGUAGES CXX C) -# Every target here, the wrapper and the cxx libraries alike, warns at the -# level cxx builds its own tests with, and every warning is an error. +# Every target here, the wrapper and the cxx libraries alike, warns at /W4 +# with MSVC and at -Wall -Wextra with gcc and clang, and every warning is an +# error. set(CMAKE_COMPILE_WARNING_AS_ERROR ON) if (MSVC) add_compile_options(/W4) else() - add_compile_options(-Wall -fPIC) + add_compile_options(-Wall -Wextra -fPIC) endif() if (MSVC) @@ -70,7 +71,7 @@ if (RebuildSwig) message(WARNING "SWIG does not include Java support so will not be run") else() message(STATUS "Rebuilding SWIG interfaces") - execute_process(COMMAND ${SWIG_EXECUTABLE} -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.ipintelligence.engine.onpremise.interop.swig -module IpIntelligenceOnPremiseEngineModule -outdir ${SRCOUT}/interop/swig -o ${CMAKE_CURRENT_LIST_DIR}/Java_Ipi_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i) + execute_process(COMMAND ${SWIG_EXECUTABLE} -Wall -Werror -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.ipintelligence.engine.onpremise.interop.swig -module IpIntelligenceOnPremiseEngineModule -outdir ${SRCOUT}/interop/swig -o ${CMAKE_CURRENT_LIST_DIR}/Java_Ipi_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i COMMAND_ERROR_IS_FATAL ANY) endif() endif() message(STATUS "Done SWIG interfaces") @@ -108,7 +109,7 @@ if (MSVC) target_compile_options(fiftyone-ip-intelligence-java PRIVATE "/W4" "/WX") target_link_options(fiftyone-ip-intelligence-java PRIVATE "/WX") else() - target_compile_options(fiftyone-ip-intelligence-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Werror") + target_compile_options(fiftyone-ip-intelligence-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Wextra" "-Werror") endif() # The wrapper includes through cxx/, so the folder that holds it is on the path. diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 index d0bc65b9b..052045ef7 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 @@ -12,7 +12,9 @@ if (Test-Path -Path Java_Ipi_Engine.cpp -PathType Leaf) { Write-Debug "Invoking SWIG executable..." +# Every SWIG warning is on and is an error, so a warning stops the run. & $swigExec ` + -Wall -Werror ` -c++ -java ` "-I../../../../.." ` -package fiftyone.ipintelligence.engine.onpremise.interop.swig ` From a4997b7f6d1fc260d69d95f522a1fe40723d23a2 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 24 Nov 2026 15:31:38 +0000 Subject: [PATCH 3/3] BUILD: Keep the SWIG wrappers when SWIG fails, and build them for gcc RegenSwig.ps1 and the -DRebuildSwig=ON configure of both engines now run SWIG into a new folder, and replace the committed wrapper and Java proxies only when SWIG succeeds. With SWIG at -Wall -Werror, a SWIG warning, such as Warning 404 for a template the cxx interface files declared twice, stopped the run with the committed files already gone. The IP Intelligence CMake run now writes its proxies to fiftyone/ipintelligence/engine/onpremise, where they are. Both Java wrapper targets build with -fno-strict-aliasing on gcc and clang, as section 27.2.4 of the SWIG Java documentation asks for an optimized gcc build, because the wrapper passes C++ pointers to Java through casts to jlong. gcc 13 had stopped both wrappers with -Werror=strict-aliasing on those casts. The cxx libraries keep strict aliasing. https://www.swig.org/Doc4.3/Java.html#Java_compiling_dynamic ci/native-warnings.Tests.ps1 checks both, and the README's SWIG command passes -Wall -Werror. --- README.md | 8 +-- ci/native-warnings.Tests.ps1 | 20 +++++-- .../src/main/cxx/CMakeLists.txt | 18 ++++++- .../src/main/cxx/CMakeLists.txt | 20 ++++++- .../src/main/cxx/RegenSwig.ps1 | 54 ++++++++++++------- 5 files changed, 93 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 628d03b57..8016fdefb 100644 --- a/README.md +++ b/README.md @@ -133,11 +133,13 @@ SWIG 4.0 or later, from this folder: ``` cd device-detection/device-detection.hash.engine.on-premise/src/main/cxx -swig -c++ -java -I../../../../.. -package fiftyone.devicedetection.hash.engine.onpremise.interop.swig -outdir ../java/fiftyone/devicedetection/hash/engine/onpremise/interop/swig -o Java_Hash_Engine.cpp hash_java.i +swig -Wall -Werror -c++ -java -I../../../../.. -package fiftyone.devicedetection.hash.engine.onpremise.interop.swig -outdir ../java/fiftyone/devicedetection/hash/engine/onpremise/interop/swig -o Java_Hash_Engine.cpp hash_java.i ``` -The IP Intelligence engine has `RegenSwig.ps1` beside its interface file that -does the same. +Every SWIG warning is an error. The IP Intelligence engine has `RegenSwig.ps1` +beside its interface file that does the same, and replaces the committed files +only when SWIG succeeds. Building either engine with `-DRebuildSwig=ON` runs +SWIG in the same way. ## Versions diff --git a/ci/native-warnings.Tests.ps1 b/ci/native-warnings.Tests.ps1 index 3e8490c18..2b8e8fa52 100644 --- a/ci/native-warnings.Tests.ps1 +++ b/ci/native-warnings.Tests.ps1 @@ -180,17 +180,31 @@ Describe 'the engine native build' -ForEach $Engines { } } + # SWIG's Java wrapper casts pointers to and from jlong, which is only + # safe without strict aliasing, so gcc does not warn about it. + It 'builds the wrapper without strict aliasing' { + (Get-Code $CMake) -join "`n" | Should -MatchExactly ( + 'target_compile_options\(\S+-java\s+PRIVATE[^)]*"-fno-strict-aliasing"') + } + It 'runs SWIG with every warning on, as an error that stops the build' { $calls = @(Get-Code $CMake | Where-Object { $_ -match '\$\{SWIG_EXECUTABLE\}' }) $calls.Count | Should -BeGreaterThan 0 foreach ($call in $calls) { $call | Should -MatchExactly '\$\{SWIG_EXECUTABLE\}\s+-Wall\s+-Werror\s' $call | Should -MatchExactly 'COMMAND_ERROR_IS_FATAL\s+ANY' + # A failed run removes its output files, so SWIG writes into the + # build folder and the committed files are replaced after it. + $call | Should -MatchExactly '-outdir\s+\$\{SWIGOUT\}/' + $call | Should -MatchExactly '\s-o\s+\$\{SWIGOUT\}/' } - # The scripts that run SWIG by hand pass the same options. + # The scripts that run SWIG by hand pass the same options, and write + # into a new folder too. foreach ($script in $Files | Where-Object { $_ -like '*.ps1' }) { - (Get-Code "$Folder/$script") -join "`n" | - Should -MatchExactly '&\s*\$swigExec[\s`]+-Wall\s+-Werror\s' + $code = (Get-Code "$Folder/$script") -join "`n" + $code | Should -MatchExactly '&\s*\$swigExec[\s`]+-Wall\s+-Werror\s' + $code | Should -MatchExactly '-outdir\s+\$TempJava' + $code | Should -MatchExactly '-o\s+\(Join-Path\s+\$Temp\s' } } diff --git a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt index 093ef29ae..875469e7d 100644 --- a/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt +++ b/device-detection/device-detection.hash.engine.on-premise/src/main/cxx/CMakeLists.txt @@ -81,7 +81,18 @@ if (RebuildSwig) message(WARNING "SWIG does not include Java support so will not be run") else() message(STATUS "Rebuilding SWIG interfaces") - execute_process(COMMAND ${SWIG_EXECUTABLE} -Wall -Werror -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.devicedetection.hash.engine.onpremise.interop.swig -outdir ${SRCOUT}/interop/swig -o ${CMAKE_CURRENT_LIST_DIR}/Java_Hash_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i COMMAND_ERROR_IS_FATAL ANY) + # SWIG writes into the build folder, and the committed wrapper and Java + # proxies are replaced only when it succeeds, as a failed run removes + # its output files. + set(SWIGOUT ${CMAKE_CURRENT_BINARY_DIR}/swig) + file(REMOVE_RECURSE ${SWIGOUT}) + file(MAKE_DIRECTORY ${SWIGOUT}/java) + execute_process(COMMAND ${SWIG_EXECUTABLE} -Wall -Werror -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.devicedetection.hash.engine.onpremise.interop.swig -outdir ${SWIGOUT}/java -o ${SWIGOUT}/Java_Hash_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i COMMAND_ERROR_IS_FATAL ANY) + file(GLOB OLD_PROXIES ${SRCOUT}/interop/swig/*.java) + file(REMOVE ${OLD_PROXIES}) + file(GLOB NEW_PROXIES ${SWIGOUT}/java/*.java) + file(COPY ${NEW_PROXIES} DESTINATION ${SRCOUT}/interop/swig) + file(COPY_FILE ${SWIGOUT}/Java_Hash_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/Java_Hash_Engine.cpp) endif() endif() message(STATUS "Done SWIG interfaces") @@ -120,6 +131,11 @@ if (MSVC) target_link_options(fiftyone-hash-java PRIVATE "/WX") else() target_compile_options(fiftyone-hash-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Wextra" "-Werror") + # The wrapper SWIG makes for Java passes C++ pointers to Java as jlong + # through pointer casts. Section 27.2.4 of the SWIG Java documentation + # says to compile it with -fno-strict-aliasing when gcc optimises, as + # those casts are not safe under strict aliasing. + target_compile_options(fiftyone-hash-java PRIVATE "-fno-strict-aliasing") endif() # The wrapper includes through cxx/, so the folder that holds it is on the path. diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt index bc6343a02..9712f8f41 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/CMakeLists.txt @@ -57,7 +57,7 @@ set(_51DEGREES_BUILD_DEVICE_DETECTION OFF CACHE BOOL "" FORCE) set(BUILD_TESTING OFF CACHE BOOL "" FORCE) include(${FIFTYONE_ROOT}/cxx/CMakeLists.txt NO_POLICY_SCOPE) -set(SRCOUT ${CMAKE_CURRENT_LIST_DIR}/../java/fiftyone/ipintelligence/hash/engine/onpremise) +set(SRCOUT ${CMAKE_CURRENT_LIST_DIR}/../java/fiftyone/ipintelligence/engine/onpremise) set(RES ${CMAKE_CURRENT_LIST_DIR}/../../../target/classes) @@ -71,7 +71,18 @@ if (RebuildSwig) message(WARNING "SWIG does not include Java support so will not be run") else() message(STATUS "Rebuilding SWIG interfaces") - execute_process(COMMAND ${SWIG_EXECUTABLE} -Wall -Werror -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.ipintelligence.engine.onpremise.interop.swig -module IpIntelligenceOnPremiseEngineModule -outdir ${SRCOUT}/interop/swig -o ${CMAKE_CURRENT_LIST_DIR}/Java_Ipi_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i COMMAND_ERROR_IS_FATAL ANY) + # SWIG writes into the build folder, and the committed wrapper and Java + # proxies are replaced only when it succeeds, as a failed run removes + # its output files. + set(SWIGOUT ${CMAKE_CURRENT_BINARY_DIR}/swig) + file(REMOVE_RECURSE ${SWIGOUT}) + file(MAKE_DIRECTORY ${SWIGOUT}/java) + execute_process(COMMAND ${SWIG_EXECUTABLE} -Wall -Werror -c++ -java -I${FIFTYONE_ROOT} -package fiftyone.ipintelligence.engine.onpremise.interop.swig -module IpIntelligenceOnPremiseEngineModule -outdir ${SWIGOUT}/java -o ${SWIGOUT}/Java_Ipi_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/hash_java.i COMMAND_ERROR_IS_FATAL ANY) + file(GLOB OLD_PROXIES ${SRCOUT}/interop/swig/*.java) + file(REMOVE ${OLD_PROXIES}) + file(GLOB NEW_PROXIES ${SWIGOUT}/java/*.java) + file(COPY ${NEW_PROXIES} DESTINATION ${SRCOUT}/interop/swig) + file(COPY_FILE ${SWIGOUT}/Java_Ipi_Engine.cpp ${CMAKE_CURRENT_LIST_DIR}/Java_Ipi_Engine.cpp) endif() endif() message(STATUS "Done SWIG interfaces") @@ -110,6 +121,11 @@ if (MSVC) target_link_options(fiftyone-ip-intelligence-java PRIVATE "/WX") else() target_compile_options(fiftyone-ip-intelligence-java PRIVATE ${COMPILE_OPTION_DEBUG} "-Wall" "-Wextra" "-Werror") + # The wrapper SWIG makes for Java passes C++ pointers to Java as jlong + # through pointer casts. Section 27.2.4 of the SWIG Java documentation + # says to compile it with -fno-strict-aliasing when gcc optimises, as + # those casts are not safe under strict aliasing. + target_compile_options(fiftyone-ip-intelligence-java PRIVATE "-fno-strict-aliasing") endif() # The wrapper includes through cxx/, so the folder that holds it is on the path. diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 index 052045ef7..8445c4660 100644 --- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 +++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 @@ -3,26 +3,44 @@ param( [string]$swigExec = "swig" ) -Write-Debug "Removing old files..." +# SWIG writes into a new folder, and the committed wrapper and Java proxies +# are replaced only when it succeeds, so a failed run leaves them as they +# were. +$ErrorActionPreference = "Stop" +$JavaOut = "../java/fiftyone/ipintelligence/engine/onpremise/interop/swig" +$Temp = Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName()) +$TempJava = Join-Path $Temp "java" +New-Item -ItemType Directory -Path $TempJava | Out-Null -Remove-Item ../java/fiftyone/ipintelligence/engine/onpremise/interop/swig/*.java -if (Test-Path -Path Java_Ipi_Engine.cpp -PathType Leaf) { - Remove-Item Java_Ipi_Engine.cpp -} +try { + Write-Debug "Invoking SWIG executable..." + + # Every SWIG warning is on and is an error, so a warning stops the run. + & $swigExec ` + -Wall -Werror ` + -c++ -java ` + "-I../../../../.." ` + -package fiftyone.ipintelligence.engine.onpremise.interop.swig ` + -module IpIntelligenceOnPremiseEngineModule ` + -outdir $TempJava ` + -o (Join-Path $Temp "Java_Ipi_Engine.cpp") ` + hash_java.i + $SwigExit = $LASTEXITCODE -Write-Debug "Invoking SWIG executable..." + Write-Debug "LASTEXITCODE = $SwigExit" -# Every SWIG warning is on and is an error, so a warning stops the run. -& $swigExec ` - -Wall -Werror ` - -c++ -java ` - "-I../../../../.." ` - -package fiftyone.ipintelligence.engine.onpremise.interop.swig ` - -module IpIntelligenceOnPremiseEngineModule ` - -outdir ../java/fiftyone/ipintelligence/engine/onpremise/interop/swig ` - -o Java_Ipi_Engine.cpp ` - hash_java.i + if ($SwigExit -ne 0) { + Write-Error "SWIG failed with exit code $SwigExit, so no file was changed." -ErrorAction Continue + exit $SwigExit + } -Write-Debug "LASTEXITCODE = $LASTEXITCODE" + Write-Debug "Replacing the old files..." -exit $LASTEXITCODE + Remove-Item "$JavaOut/*.java" + Move-Item (Join-Path $TempJava "*.java") $JavaOut + Move-Item -Force (Join-Path $Temp "Java_Ipi_Engine.cpp") "Java_Ipi_Engine.cpp" + exit 0 +} +finally { + Remove-Item -Recurse -Force $Temp +}