Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/workflows/ci-script-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -14,13 +16,17 @@ 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
- rc
paths:
- 'ci/**'
- '.github/workflows/**'
- '**/src/main/cxx/**'
- 'cxx'
workflow_dispatch:

permissions:
Expand All @@ -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: |
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
244 changes: 244 additions & 0 deletions ci/native-warnings.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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 = @(
'(?<![\w-])[/-]wd\d+'
'(?<![\w-])[/-]w(?![\w-])'
'(?<![\w-])-Wno-'
'_CRT_SECURE_NO_WARNINGS'
'(?<!SWIG_NO)_CRT_SECURE_NO_DEPRECATE'
'_SCL_SECURE_NO_WARNINGS'
'(?<!SWIG_NO)_SCL_SECURE_NO_DEPRECATE'
'pragma\s+warning\s*\(\s*disable'
'diagnostic\s+ignored'
)
}

Describe 'the <Engine> 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 <Wrapper> 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+(?<v>\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
}
}
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading