Skip to content

Commit f7eb9d7

Browse files
committed
feat platform: optimize cross-compile by using try compile to determine types
Signed-off-by: John Sanpe <sanpeqf@gmail.com>
1 parent 289d095 commit f7eb9d7

4 files changed

Lines changed: 71 additions & 79 deletions

File tree

scripts/compare-type.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Copyright(c) 2023 John Sanpe <sanpeqf@gmail.com>
4+
*/
5+
6+
#include <stdint.h>
7+
#include <stddef.h>
8+
9+
#if !defined(TYPE1) || !defined(TYPE2)
10+
# error "Undefine types"
11+
#endif
12+
13+
#define typecheck(type, object) ({ \
14+
type __dummy1; \
15+
typeof(object) __dummy2; \
16+
(void)(&__dummy1 == &__dummy2); \
17+
1; \
18+
})
19+
20+
int
21+
main(int argc, const char *argv[])
22+
{
23+
typecheck(TYPE1, (TYPE2){0});
24+
return 0;
25+
}

scripts/get-type.c

Lines changed: 0 additions & 39 deletions
This file was deleted.

scripts/get-type.cmake

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# SPDX-License-Identifier: GPL-2.0-or-later
2+
#
3+
# Copyright(c) 2025 John Sanpe <sanpeqf@gmail.com>
4+
#
5+
6+
function(compare_type result type1 type2)
7+
message(DEBUG "Compare type: '${type1}' '${type2}'")
8+
try_compile(
9+
COMPILE_RESULT
10+
${CMAKE_CURRENT_BINARY_DIR}
11+
${CMAKE_CURRENT_LIST_DIR}/compare-type.c
12+
COMPILE_DEFINITIONS "-Werror -Wcompare-distinct-pointer-types"
13+
COMPILE_DEFINITIONS "-DTYPE1=${type1}"
14+
COMPILE_DEFINITIONS "-DTYPE2=${type2}"
15+
OUTPUT_VARIABLE OUTPUT
16+
)
17+
set(${result} ${COMPILE_RESULT} PARENT_SCOPE)
18+
endfunction()
19+
20+
function(get_type result name default)
21+
message(CHECK_START "Looking for type '${name}'")
22+
set(candidates "int" "long" "long long" "char" "short")
23+
24+
foreach(walk IN LISTS candidates)
25+
compare_type(compare "${name}" "${walk}")
26+
if(NOT ${compare})
27+
compare_type(compare "${name}" "unsigned ${walk}")
28+
endif()
29+
30+
if(${compare})
31+
set(type "${walk}")
32+
message(CHECK_PASS "found '${walk}'")
33+
break()
34+
endif()
35+
endforeach()
36+
37+
if(NOT type)
38+
message(CHECK_FAIL "use default '${default}'")
39+
set(type "${default}")
40+
endif()
41+
set(${result} "${type}" PARENT_SCOPE)
42+
endfunction()

scripts/platform.cmake

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,6 @@
33
# Copyright(c) 2025 John Sanpe <sanpeqf@gmail.com>
44
#
55

6-
if(HOST_C_COMPILER)
7-
set(PRINT_TYPE_LINK "-static")
8-
endif()
9-
10-
macro(print_type output name)
11-
message(STATUS "Print type: " ${name})
12-
try_run(
13-
RUN_RESULT
14-
COMPILE_RESULT
15-
${CMAKE_CURRENT_BINARY_DIR}
16-
${CMAKE_CURRENT_LIST_DIR}/get-type.c
17-
COMPILE_DEFINITIONS "-Werror -DTYPE=${name}"
18-
LINK_OPTIONS "${PRINT_TYPE_LINK}"
19-
RUN_OUTPUT_VARIABLE ${output}
20-
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
21-
)
22-
if ((NOT COMPILE_RESULT) OR (NOT RUN_RESULT EQUAL 0))
23-
message("Compile output: ${COMPILE_OUTPUT}")
24-
message("Run result: ${RUN_RESULT}")
25-
message(FATAL_ERROR "Failed to retrieve the original type ${name}")
26-
endif()
27-
endmacro()
28-
296
if(CMAKE_SYSTEM_NAME MATCHES "Linux|Android|Darwin|GNU")
307
set(BFDEV_PORT_TYPE "posix")
318
endif()
@@ -42,20 +19,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "Generic")
4219
set(BFDEV_PORT_TYPE "generic")
4320
endif()
4421

45-
if(BFDEV_PORT_TYPE STREQUAL "posix")
46-
print_type(BFDEV_TYPE_ADDR "size_t")
47-
print_type(BFDEV_TYPE_W64 "int64_t")
48-
print_type(BFDEV_TYPE_MAX "intmax_t")
49-
endif()
50-
51-
if(NOT BFDEV_TYPE_ADDR)
52-
set(BFDEV_TYPE_ADDR "long")
53-
endif()
54-
55-
if(NOT BFDEV_TYPE_W64)
56-
set(BFDEV_TYPE_W64 "long long")
57-
endif()
58-
59-
if(NOT BFDEV_TYPE_MAX)
60-
set(BFDEV_TYPE_MAX "long long")
61-
endif()
22+
include(${CMAKE_CURRENT_LIST_DIR}/get-type.cmake)
23+
get_type(BFDEV_TYPE_ADDR "size_t" "long")
24+
get_type(BFDEV_TYPE_W64 "int64_t" "long long")
25+
get_type(BFDEV_TYPE_MAX "intmax_t" "long long")

0 commit comments

Comments
 (0)