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
140 changes: 140 additions & 0 deletions ci/native-warnings.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Tests that the two on-premise packages build their native code, the SWIG
# wrapper, the C++ and the C, with the warnings of -Wall -Wextra and /W4 on and
# treated as errors, and that nothing turns a warning off: no -Wno-* or /wd
# flag, no lower warning level, no C runtime define that turns deprecation
# warnings off, no commented out -Werror or /WX, and no pragma in a wrapper
# other than those SWIG writes and the interface file lists.
# setup.py, the interface files and the wrappers are read as text.

BeforeDiscovery {
$script:Packages = @(
@{
Name = 'fiftyone_devicedetection_onpremise'
Folder = 'device-detection/fiftyone_devicedetection_onpremise'
Interface = 'hash_python.i'
Wrapper = 'hash_python_wrap.cxx'
},
@{
Name = 'fiftyone_ipintelligence_onpremise'
Folder = 'ip-intelligence/fiftyone_ipintelligence_onpremise'
Interface = 'ipi_python.i'
Wrapper = 'ipi_python_wrap.cxx'
}
)
}

Describe '<Name> native build flags' -ForEach $Packages {
BeforeAll {
$root = Join-Path $PSScriptRoot '..' $Folder
$script:Lines = @(Get-Content -LiteralPath (Join-Path $root 'setup.py'))
# The lines that are code, with any comment at the end taken off.
$script:Code = @($script:Lines |
Where-Object { $_ -notmatch '^\s*#' } |
ForEach-Object { $_ -replace '\s#.*$', '' })
$script:Comments = @($script:Lines | Where-Object { $_ -match '^\s*#' })

function Get-Count([string]$Pattern) {
@($script:Code | Where-Object { $_ -match $Pattern }).Count
}
}

It 'turns no warning off and lowers no warning level' {
$switches = @($script:Code | Where-Object {
$_ -match "-Wno-|/wd\d|/w[1-4]\d|'-w'|'/w'|'/W[0-3]'|/external:W0"
})
$switches | Should -BeNullOrEmpty
}

It 'defines nothing that turns off the C runtime deprecation warnings' {
$defines = @($script:Code | Where-Object {
$_ -match '_CRT_SECURE_NO_WARNINGS|(?<!SWIG_NO)_CRT_SECURE_NO_DEPRECATE|_CRT_NONSTDC_NO_DEPRECATE'
})
$defines | Should -BeNullOrEmpty
}

It 'has no -Werror or /WX commented out' {
$commented = @($script:Comments | Where-Object {
$_ -match "'(-Werror|/WX)'"
})
$commented | Should -BeNullOrEmpty
}

It 'asks for -Wall, -Wextra and -Werror for the C++ and the C' {
Get-Count "'-Wall'" | Should -BeGreaterOrEqual 2
Get-Count "'-Wextra'" | Should -BeGreaterOrEqual 2
Get-Count "'-Werror'" | Should -BeGreaterOrEqual 2
}

It 'asks for /W4 and /WX for the C++ and the C, and /WX for the link' {
Get-Count "'/W4'" | Should -BeGreaterOrEqual 2
Get-Count "'/WX'" | Should -BeGreaterOrEqual 3
}

It 'takes the warning level setuptools gives MSVC out, so /W4 is the only one' {
# cl warns (D9025) when /W4 overrides another level, and /WX does not
# make that an error.
Get-Count '^\s*def remove_msvc_warning_level\(compiler\):' |
Should -Be 1
Get-Count 'compiler\.compile_options,\s*compiler\.compile_options_debug' |
Should -Be 1
Get-Count '^\s*remove_msvc_warning_level\(self\.compiler\)' |
Should -Be 2
Get-Count '^\s*cmdclass=\{"build_ext":\s*NoSuffixBuilder,\s*"build_clib":\s*LibraryBuilder\}' |
Should -Be 1
}

It 'gives the wrapper its own flags' {
Get-Count '^\s*extra_compile_args=extra_compile_args,' |
Should -Be 1
}
}

Describe '<Name> SWIG wrapper' -ForEach $Packages {
BeforeAll {
$source = Join-Path $PSScriptRoot '..' $Folder 'src' $Name
$script:Interface = Get-Content -Raw `
-LiteralPath (Join-Path $source $Interface)
$script:WrapperLines = @(Get-Content `
-LiteralPath (Join-Path $source $Wrapper))
}

It 'asks SWIG to leave on the warnings it would turn off' {
foreach ($name in 'SWIG_MSC_UNSUPPRESS_4505',
'SWIG_NO_CRT_SECURE_NO_DEPRECATE',
'SWIG_NO_SCL_SECURE_NO_DEPRECATE') {
$script:Interface | Should -Match "(?m)^#define $name\s*$"
}
}

It 'has no pragma other than those the interface file lists' {
# The pragmas SWIG writes that no SWIG setting removes, each listed in
# the interface file comment with the reason it changes nothing here,
# and the 4505 one, which SWIG_MSC_UNSUPPRESS_4505 leaves out.
$known = @(
'#pragma warning(disable : 4505)'
'#pragma warning disable 592'
'#pragma GCC diagnostic push'
'#pragma GCC diagnostic pop'
'#pragma GCC diagnostic ignored "-Wregister"'
'#pragma GCC diagnostic ignored "-Wdeprecated"'
'#pragma GCC diagnostic ignored "-Wdeprecated-declarations"'
'#pragma warning(push)'
'#pragma warning(pop)'
'#pragma warning(disable: 4996)'
)
$other = @($script:WrapperLines |
Where-Object { $_ -match '^\s*#\s*pragma\b' } |
ForEach-Object {
($_ -replace '/\*.*$', '' -replace '^\s*#\s*', '#').Trim()
} |
Where-Object { $_ -notin $known })
$other | Should -BeNullOrEmpty
}

It 'lists each SWIG pragma in the interface file comment' {
foreach ($text in '-Wregister', '4505', '592', '-Wdeprecated',
'4996') {
$script:Interface | Should -Match ([regex]::Escape($text))
}
}
}
2 changes: 1 addition & 1 deletion cxx
Submodule cxx updated 109 files
58 changes: 35 additions & 23 deletions device-detection/fiftyone_devicedetection_onpremise/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import platform
from setuptools import setup, Extension
from setuptools.command import develop
from setuptools.command.build_clib import build_clib
from distutils import sysconfig
from Cython.Distutils import build_ext
from glob import glob
import os
import io
import re
import shutil

# Read a text file and return the content as a string.
Expand Down Expand Up @@ -72,7 +74,28 @@ def get_version():
python_version = str(sys.version_info[0])
return version + python_version

def remove_msvc_warning_level(compiler):

"""Take the warning level that setuptools gives MSVC out of its options,
so /W4 below is the only level. cl warns (D9025) when one level overrides
another, and /WX does not make that warning an error."""
if compiler.compiler_type != "msvc":
return
if not compiler.initialized:
compiler.initialize()
for options in (compiler.compile_options, compiler.compile_options_debug):
options[:] = [o for o in options if not re.fullmatch(r"/W\d", o)]

class LibraryBuilder(build_clib):
def build_libraries(self, libraries):
remove_msvc_warning_level(self.compiler)
super().build_libraries(libraries)

class NoSuffixBuilder(build_ext):
def build_extensions(self):
remove_msvc_warning_level(self.compiler)
super().build_extensions()

def get_ext_filename(self, ext_name):
filename = super().get_ext_filename(ext_name)
suffix = sysconfig.get_config_var('EXT_SUFFIX')
Expand Down Expand Up @@ -129,44 +152,40 @@ def stage_native_source():
# Extra compilation flags for C library
cflags = []

# The warnings of -Wall -Wextra and /W4 are on and are errors, for the
# wrapper, the C++ and the C alike. -Wunused-result comes after the flags
# Python was built with, which can turn it off.
if sys.platform != "win32":
extra_compile_args.extend([
'-fPIC',
'-std=gnu++17',
'-Wall',
# '-Werror'
'-Wextra',
'-Wunused-result',
'-Werror'
])
cflags.extend([
'-std=gnu11',
'-Wall',
# '-Werror',
'-Wno-strict-prototypes',
'-Wno-unused-variable',
'-Wno-missing-braces',
'-Wno-strict-aliasing'
'-Wextra',
'-Wunused-result',
'-Werror'
])


if sys.platform == "win32":
extra_compile_args.extend([
'/std:c++17',
'/D_CRT_SECURE_NO_WARNINGS',
'/D_UNICODE',
'/DUNICODE',
'/W4',
# '/WX',
'/wd4127',
'/wd4456',
'/wd4701',
'/wd4703',
'/wd4706'
'/WX'
])
cflags.extend([
'/D_CRT_SECURE_NO_WARNINGS',
'/D_UNICODE',
'/DUNICODE',
'/W4',
# '/WX'
'/WX'
])
extra_link_args.extend([
'/WX'
Expand All @@ -182,13 +201,6 @@ def stage_native_source():
extra_compile_args.extend([
'-DGCC_ENABLE_CPP_EXCEPTIONS=YES'
])
if sys.version_info[0] == 3 and sys.version_info[1] == 8:
extra_compile_args.extend([
'-Wno-deprecated-declarations'
])
cflags.extend([
'-Wno-atomic-alignment'
])

DeviceDetectionHashEngineModule = Extension('_DeviceDetectionHashEngineModule',
sources=[
Expand Down Expand Up @@ -227,7 +239,7 @@ def stage_native_source():
})

setup(
cmdclass={"build_ext": NoSuffixBuilder},
cmdclass={"build_ext": NoSuffixBuilder, "build_clib": LibraryBuilder},
name="fiftyone_devicedetection_onpremise",
version=read("version.txt"),
author="51Degrees Engineering",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

/* The wrapper is built with the warnings of -Wall -Wextra and /W4 as errors,
* so SWIG is asked to leave on the warnings it turns off for the whole wrapper
* (C4505 and the C runtime deprecation warnings). SWIG's runtime defines
* helper functions that a module may not call. GCC and Clang are told so by
* SWIG's unused attribute, which MSVC has no equivalent for, so on MSVC they
* are inline, as unused inline functions are expected. Only the arguments
* that differ are checked to tell the overloads apart (fast dispatch), which
* also keeps each check in a scope of its own so no local hides another.
*
* SWIG still writes these pragmas in the wrapper, and no SWIG setting
* removes them. None of them changes this build:
* - GCC "-Wregister", around Python.h only, for Python 2.7 headers. The
* Python headers this package builds with use register only for GNU
* explicit register variables, which -Wregister does not report.
* - Intel compiler warning 592, for the Intel compiler only.
* - GCC "-Wdeprecated" and "-Wdeprecated-declarations", and MSVC 4996,
* around PyObject_AsReadBuffer, only when SWIG_PYTHON_OLD_BUFFER_PROTOCOL
* is defined. This build does not define it.
* ci/native-warnings.Tests.ps1 fails on any other pragma in the wrapper. */
%begin %{
#define SWIG_MSC_UNSUPPRESS_4505
#define SWIG_NO_CRT_SECURE_NO_DEPRECATE
#define SWIG_NO_SCL_SECURE_NO_DEPRECATE
#if defined(_MSC_VER)
#define SWIGINTERN static inline
#define SWIGINTERNINLINE static inline
#define SWIGRUNTIMEINLINE static inline
#endif
%}
%feature("fastdispatch");

%include <pybuffer.i>
%pybuffer_binary(unsigned char data[], long length);
%rename(refreshDataFromFile) refreshData(const char *fileName);
Expand Down
Loading
Loading