Skip to content

Commit 62f12cc

Browse files
committed
Refactor and fix setting basisu GCC/Clang compiler options
There were bugs where get_source_file_property() was called on a different file than set_source_file_properties() were called on afterwards, which lead to eat least one new compiler warning. A helper function makes this more readable and less error-prone, and allows appending an option to multiple files without overwriting their existing options.
1 parent 39c9a70 commit 62f12cc

File tree

1 file changed

+56
-60
lines changed

1 file changed

+56
-60
lines changed

CMakeLists.txt

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,30 @@ PRIVATE
837837
BASISD_SUPPORT_KTX2=0
838838
)
839839

840+
# helper function to append COMPILE_OPTIONS to source file properties
841+
# !! if SRCFILES is a list, remember to quote it !!
842+
# !! like add_source_file_compile_options("${MY_SRC_LIST}" "-Wall") !!
843+
# !! or add_source_file_compile_options("dir/foo.cpp;dir/bar.cpp" "-Wextra") !!
844+
function(add_source_file_compile_options SRCFILES OPTIONS)
845+
foreach(src_file ${SRCFILES})
846+
get_source_file_property(cur_options
847+
"${src_file}"
848+
COMPILE_OPTIONS
849+
)
850+
if(cur_options)
851+
set_source_files_properties(
852+
"${src_file}"
853+
PROPERTIES COMPILE_OPTIONS "${cur_options};${OPTIONS}"
854+
)
855+
else() # if cur_options were undefined or empty ("")
856+
set_source_files_properties(
857+
"${src_file}"
858+
PROPERTIES COMPILE_OPTIONS "${OPTIONS}"
859+
)
860+
endif()
861+
endforeach()
862+
endfunction()
863+
840864
# Turn off these warnings until Rich fixes the occurences.
841865
# It it not clear to me if generator expressions can be used here
842866
# hence the long-winded way.
@@ -847,55 +871,39 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
847871
# It's too much work to discriminate which files need which warnings
848872
# disabled.
849873
${BASISU_ENCODER_CXX_SRC}
850-
PROPERTIES COMPILE_OPTIONS "-Wno-sign-compare;-Wno-unused-variable;-Wno-class-memaccess;-Wno-misleading-indentation;-Wno-extra;-Wno-deprecated-copy;-Wno-parentheses"
874+
PROPERTIES COMPILE_OPTIONS "-fno-strict-aliasing;-Wno-sign-compare;-Wno-unused-variable;-Wno-class-memaccess;-Wno-misleading-indentation;-Wno-extra;-Wno-deprecated-copy;-Wno-parentheses"
851875
)
852876
set_source_files_properties(
853877
external/basisu/transcoder/basisu_transcoder.cpp
854-
PROPERTIES COMPILE_OPTIONS "-Wno-sign-compare;-Wno-unused-function;-Wno-unused-variable;-Wno-class-memaccess;-Wno-maybe-uninitialized"
878+
PROPERTIES COMPILE_OPTIONS "-fno-strict-aliasing;-Wno-sign-compare;-Wno-unused-function;-Wno-unused-variable;-Wno-class-memaccess;-Wno-maybe-uninitialized"
855879
)
856880
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "11")
857881
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS "12" )
858-
# Version 11 raises several stringop-overflow warnings in some
859-
# very hard to decipher code. They appear to be bogus based on
860-
# the facts that we have never seen a crash and version 12 no
861-
# longer raises the warnings.
862-
get_source_file_property(cur_options
863-
external/basisu/encoder/basisu_comp.cpp
864-
COMPILE_OPTIONS
865-
)
866-
set_source_files_properties(
867-
external/basisu/encoder/basisu_comp.cpp
868-
PROPERTIES COMPILE_OPTIONS "${cur_options};-Wno-stringop-overflow"
869-
)
882+
# Version 11 raises several stringop-overflow warnings in some
883+
# very hard to decipher code. They appear to be bogus based on
884+
# the facts that we have never seen a crash and version 12 no
885+
# longer raises the warnings.
886+
add_source_file_compile_options(
887+
external/basisu/encoder/basisu_comp.cpp
888+
"-Wno-stringop-overflow"
889+
)
870890
endif()
871891
endif()
872892
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "12.0")
873893
# Version 12 newly raises this warning on basisu_uastc_enc.cpp.
874894
# There seems no way for the index calculated by the code at
875895
# line 326, where the error is raised, to be > the array length.
876896
# Also we have never seen any crashes.
877-
set_source_files_properties(
897+
add_source_file_compile_options(
878898
external/basisu/encoder/basisu_uastc_enc.cpp
879-
PROPERTIES COMPILE_OPTIONS "-Wno-stringop-overflow"
899+
"-Wno-stringop-overflow"
880900
)
881901
endif()
882902
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "14.0")
883903
# Version 14 raises stringop-overflow on these files.
884-
get_source_file_property(cur_options
885-
external/basisu/encoder/basisu_comp.cpp
886-
COMPILE_OPTIONS
887-
)
888-
set_source_files_properties(
889-
external/basisu/transcoder/basisu_transcoder.cpp
890-
PROPERTIES COMPILE_OPTIONS "${cur_options};-Wno-stringop-overflow"
891-
)
892-
get_source_file_property(cur_options
893-
external/basisu/encoder/basisu_bc7enc.cpp
894-
COMPILE_OPTIONS
895-
)
896-
set_source_files_properties(
897-
external/basisu/encoder/basisu_bc7enc.cpp
898-
PROPERTIES COMPILE_OPTIONS "${cur_options};-Wno-stringop-overflow"
904+
add_source_file_compile_options(
905+
"external/basisu/transcoder/basisu_transcoder.cpp;external/basisu/encoder/basisu_bc7enc.cpp"
906+
"-Wno-stringop-overflow"
899907
)
900908
endif()
901909
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
@@ -918,23 +926,31 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
918926
set( clang_version ${CMAKE_CXX_COMPILER_VERSION} )
919927
endif()
920928
# BEWARE: set_source_files_properties is not additive; it replaces.
929+
set_source_files_properties(
930+
${BASISU_ENCODER_CXX_SRC}
931+
PROPERTIES COMPILE_OPTIONS "-fno-strict-aliasing"
932+
)
933+
set_source_files_properties(
934+
external/basisu/transcoder/basisu_transcoder.cpp
935+
PROPERTIES COMPILE_OPTIONS "-fno-strict-aliasing"
936+
)
921937
if (${clang_version} VERSION_GREATER_EQUAL "12.0.0")
922-
set_source_files_properties( external/basisu/encoder/basisu_kernels_sse.cpp
923-
PROPERTIES COMPILE_OPTIONS "-Wno-unused-parameter;-Wno-deprecated-copy;-Wno-uninitialized-const-reference"
938+
add_source_file_compile_options( external/basisu/encoder/basisu_kernels_sse.cpp
939+
"-Wno-unused-parameter;-Wno-deprecated-copy;-Wno-uninitialized-const-reference"
924940
)
925941
else()
926-
set_source_files_properties( external/basisu/encoder/basisu_kernels_sse.cpp
927-
PROPERTIES COMPILE_OPTIONS "-Wno-unused-parameter"
942+
add_source_file_compile_options( external/basisu/encoder/basisu_kernels_sse.cpp
943+
"-Wno-unused-parameter"
928944
)
929945
endif()
930946
if (${clang_version} VERSION_GREATER_EQUAL "14.0")
931-
set_source_files_properties(
932-
${BASISU_ENCODER_CXX_SRC}
933-
PROPERTIES COMPILE_OPTIONS "-Wno-sign-compare;-Wno-unused-variable;-Wno-unused-parameter;-Wno-deprecated-copy-with-user-provided-copy"
947+
add_source_file_compile_options(
948+
"${BASISU_ENCODER_CXX_SRC}"
949+
"-Wno-sign-compare;-Wno-unused-variable;-Wno-unused-parameter;-Wno-deprecated-copy-with-user-provided-copy"
934950
)
935-
set_source_files_properties(
951+
add_source_file_compile_options(
936952
external/basisu/transcoder/basisu_transcoder.cpp
937-
PROPERTIES COMPILE_OPTIONS "-Wno-sign-compare;-Wno-unused-function;-Wno-unused-variable"
953+
"-Wno-sign-compare;-Wno-unused-function;-Wno-unused-variable"
938954
)
939955
set_source_files_properties(
940956
external/basisu/zstd/zstd.c
@@ -945,26 +961,6 @@ else()
945961
message(FATAL_ERROR "${CMAKE_CXX_COMPILER_ID} not yet supported.")
946962
endif()
947963

948-
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
949-
# Basis Universal requires strict aliasing to be disabled for GCC and Clang
950-
get_source_file_property(cur_options
951-
external/basisu/encoder/basisu_comp.cpp
952-
COMPILE_OPTIONS
953-
)
954-
set_source_files_properties(
955-
${BASISU_ENCODER_CXX_SRC}
956-
PROPERTIES COMPILE_OPTIONS "${cur_options};-fno-strict-aliasing"
957-
)
958-
get_source_file_property(cur_options
959-
external/basisu/transcoder/basisu_transcoder.cpp
960-
COMPILE_OPTIONS
961-
)
962-
set_source_files_properties(
963-
external/basisu/transcoder/basisu_transcoder.cpp
964-
PROPERTIES COMPILE_OPTIONS "${cur_options};-fno-strict-aliasing"
965-
)
966-
endif()
967-
968964
# Retrieve the final set of properties for use by the transcodetests.
969965
# We do this because the CMake feature that would allow the transcodetests
970966
# target to retrieve these from the ktx target is not available until

0 commit comments

Comments
 (0)