diff --git a/.github/workflows/ci-script-tests.yml b/.github/workflows/ci-script-tests.yml index de3989ad9..2dd01c9e1 100644 --- a/.github/workflows/ci-script-tests.yml +++ b/.github/workflows/ci-script-tests.yml @@ -4,7 +4,9 @@ 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 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: @@ -14,6 +16,8 @@ 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/**' + - 'cxx' push: branches: - main @@ -21,6 +25,8 @@ on: paths: - 'ci/**' - '.github/workflows/**' + - '**/src/main/cxx/**' + - 'cxx' workflow_dispatch: permissions: @@ -40,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/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 new file mode 100644 index 000000000..2b8e8fa52 --- /dev/null +++ b/ci/native-warnings.Tests.ps1 @@ -0,0 +1,244 @@ +# Pester 5 tests of the warnings the two native engines build with. Every C and +# 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 = @( + @{ + 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, + # 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|::)') { $_ } + } 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) { + 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 $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' { + $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 -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) + '"') + } + } + + # 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, and write + # into a new folder too. + foreach ($script in $Files | Where-Object { $_ -like '*.ps1' }) { + $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' + } + } + + 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' + } +} + +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 36b35dca0..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 @@ -1,16 +1,20 @@ -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) +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 /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(/wd5105 /wd4100) + add_compile_options(/W4) else() - add_compile_options(-fPIC) -# if (NOT Apple) -# endif() + add_compile_options(-Wall -Wextra -fPIC) endif() if (MSVC) @@ -77,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} -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) + # 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") @@ -106,10 +121,21 @@ 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" "-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/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..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 @@ -1,16 +1,20 @@ -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) +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 /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(/wd5105 /wd4100) + add_compile_options(/W4) else() - add_compile_options(-fPIC) -# if (NOT Apple) -# endif() + add_compile_options(-Wall -Wextra -fPIC) endif() if (MSVC) @@ -53,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) @@ -67,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} -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) + # 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") @@ -96,10 +111,21 @@ 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" "-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/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/RegenSwig.ps1 b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/cxx/RegenSwig.ps1 index d0bc65b9b..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,24 +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" -& $swigExec ` - -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 +} 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)); } %}