Skip to content

Commit

Permalink
Properly integrate new type check with new cmake layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzmbrzl committed Jan 3, 2025
1 parent 9244c88 commit af92da7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 54 deletions.
53 changes: 0 additions & 53 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,59 +118,6 @@ if (SOCI_TESTS)
add_subdirectory(tests)
endif()

# TODO: Move away in subdirectory
###############################################################################
# platform-dependent settings
###############################################################################

include(CheckTypeSize)
check_type_size(long SOCI_SIZEOF_LONG LANGUAGE CXX)
set(SOCI_SIZEOF_LONG ${SOCI_SIZEOF_LONG} CACHE INTERNAL "Size of long")

include(CheckCXXSourceCompiles)

function(soci_check_types_are_same type1 type2)
string(TOUPPER ${type1} type1_upper)
string(MAKE_C_IDENTIFIER ${type1_upper} type1_id)
string(TOUPPER ${type2} type2_upper)
string(MAKE_C_IDENTIFIER ${type2_upper} type2_id)

check_cxx_source_compiles("
#include <cstdint>
struct Foo {
void foo(${type1} x);
void foo(${type2} x);
};
int main() { return 0; }
"
"SOCI_TYPES_${type1_id}_AND_${type2_id}_DIFFER"
)

if(SOCI_TYPES_${type1_id}_AND_${type2_id}_DIFFER)
set(soci_types_equal FALSE)
else()
set(soci_types_equal TRUE)
endif()

# TODO: This needs to define the respective macro instead of a cmake variable
set("SOCI_${type1_id}_IS_${type2_id}" ${soci_types_equal} CACHE INTERNAL "${type1} and ${type2} are the same type")
endfunction()

if(SOCI_SIZEOF_LONG EQUAL 8)
# int64_t can be defined as either long or long long in this case, find out
# which one we have.
soci_check_types_are_same("int64_t" "long")
elseif(SOCI_SIZEOF_LONG EQUAL 4)
# Nothing to do in this case, actually, as it's never going to be the same
# as int32_t which is defined as int and not long in this case.
else()
message(FATAL_ERROR "Unsupported size of long=${SOCI_SIZEOF_LONG}")
endif()

soci_check_types_are_same("int8_t" "char")


# Packaging
include(CMakePackageConfigHelpers)
Expand Down
46 changes: 46 additions & 0 deletions cmake/soci_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,50 @@ function(soci_public_dependency)
endforeach()
endfunction()

# This function can be used to check whether two C++ types actually refer to the same
# type (e.g. if one is aliased to the other).
#
# Use as
# soci_are_types_same(
# TYPES <type1> <type2> [... <typeN>]
# OUTPUT_VARIABLE <name>
# )
# where
# - <type1>, <type2>, <typeN> are the types to test
# - <name> is the name of the variable that will hold the result of the check
function(soci_are_types_same)
set(FLAGS "")
set(ONE_VAL_OPTIONS "OUTPUT_VARIABLE")
set(MULTI_VAL_OPTIONS "TYPES")

cmake_parse_arguments(TYPES_SAME "${FLAGS}" "${ONE_VAL_OPTIONS}" "${MULTI_VAL_OPTIONS}" ${ARGV})
soci_verify_parsed_arguments(
PREFIX "TYPES_SAME"
FUNCTION_NAME "soci_are_types_same"
REQUIRED "TYPES" "OUTPUT_VARIABLE"
)

set(TEST_CODE "#include <cstdint>\nstruct Foo { ")
set(TEST_NAME "")
foreach(CURRENT_TYPE IN LISTS TYPES_SAME_TYPES)
string(APPEND TEST_CODE "void foo(${CURRENT_TYPE} x); ")
string(TOUPPER "${CURRENT_TYPE}" UPPER_TYPE)
string(APPEND TEST_NAME "${UPPER_TYPE}_")
endforeach()
string(APPEND TEST_CODE "};\nint main() {}")
string(APPEND TEST_NAME "ARE_DISTINGUISHABLE")

include(CheckCXXSourceCompiles)

# If some of the provided types are actually the same, compilation
# will fail due to duplication of function declarations.
check_cxx_source_compiles("${TEST_CODE}" ${TEST_NAME})

if (${TEST_NAME})
set("${TYPES_SAME_OUTPUT_VARIABLE}" FALSE PARENT_SCOPE)
else()
set("${TYPES_SAME_OUTPUT_VARIABLE}" TRUE PARENT_SCOPE)
endif()
endfunction()

set(SOCI_UTILS_ALREADY_INCLUDED TRUE)
23 changes: 22 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ add_subdirectory(core)
add_subdirectory(backends)


###############################################################################
# platform-dependent settings
###############################################################################

include(CheckTypeSize)
check_type_size(long SOCI_SIZEOF_LONG LANGUAGE CXX)

soci_are_types_same(TYPES "int64_t" "long" OUTPUT_VARIABLE SOCI_INT64_T_IS_LONG)
soci_are_types_same(TYPES "int8_t" "char" OUTPUT_VARIABLE SOCI_INT8_T_IS_CHAR)

include(CheckCXXSourceCompiles)
check_cxx_source_compiles(
"
Expand All @@ -34,6 +44,9 @@ set(CONFIG_VARS
SOCI_POSTGRESQL
SOCI_SQLITE3
SOCI_VISIBILITY_ATTRIBUTE_SUPPORTED
SOCI_SIZEOF_LONG
SOCI_INT64_T_IS_LONG
SOCI_INT8_T_IS_CHAR
)
set(CONFIG_MACROS
SOCI_HAVE_EMPTY
Expand All @@ -45,6 +58,9 @@ set(CONFIG_MACROS
SOCI_HAVE_POSTGRESQL
SOCI_HAVE_SQLITE3
SOCI_HAVE_VISIBILITY_SUPPORT
SOCI_SIZEOF_LONG
SOCI_INT64_T_IS_LONG
SOCI_INT8_T_IS_CHAR
)

list(LENGTH CONFIG_MACROS N_CONFIGS)
Expand All @@ -55,7 +71,12 @@ foreach (I RANGE ${LAST_CONFIG_IDX})
list(GET CONFIG_MACROS ${I} CURRENT_MACRO)

if (${CURRENT_VAR})
list(APPEND CONFIG_LINES "#define ${CURRENT_MACRO}")
if ("${${CURRENT_VAR}}" MATCHES "[0-9]+")
list(APPEND CONFIG_LINES "#define ${CURRENT_MACRO} ${${CURRENT_VAR}}")
else()
# Assume this is a boolean flag
list(APPEND CONFIG_LINES "#define ${CURRENT_MACRO} 1")
endif()
else()
list(APPEND CONFIG_LINES "/* #define ${CURRENT_MACRO} */")
endif()
Expand Down

0 comments on commit af92da7

Please sign in to comment.