|
| 1 | +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) |
| 2 | + |
| 3 | +project(lc C CXX) |
| 4 | + |
| 5 | +if (NOT CMAKE_BUILD_TYPE) |
| 6 | + set(CMAKE_BUILD_TYPE Debug |
| 7 | + CACHE STRING "Build type (Debug, Release)" FORCE) |
| 8 | +endif () |
| 9 | +if (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR |
| 10 | + CMAKE_BUILD_TYPE STREQUAL "Release")) |
| 11 | + message("${CMAKE_BUILD_TYPE}") |
| 12 | + message(FATAL_ERROR "CMAKE_BUILD_TYPE must be one of: Debug, Release (current value: '${CMAKE_BUILD_TYPE}')") |
| 13 | +endif () |
| 14 | + |
| 15 | +find_program(LC NAMES lc) |
| 16 | + |
| 17 | +set(LC_RTL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime) |
| 18 | +set(LC_RTL_HEADER_DIR ${LC_RTL_DIR}/impure) |
| 19 | + |
| 20 | +find_path(LC_RTLIB_DIR lfortran_intrinsics.h |
| 21 | + HINTS ${LC_RTL_HEADER_DIR}) |
| 22 | +find_library(LC_RTLIB_LIBRARY lc_runtime_static |
| 23 | + HINTS ${LC_RTL_DIR}) |
| 24 | +add_library(lc_runtime INTERFACE IMPORTED) |
| 25 | +set_property(TARGET lc_runtime PROPERTY INTERFACE_INCLUDE_DIRECTORIES |
| 26 | + ${LC_RTLIB_DIR}) |
| 27 | +set_property(TARGET lc_runtime PROPERTY INTERFACE_LINK_LIBRARIES |
| 28 | + ${LC_RTLIB_LIBRARY}) |
| 29 | +target_link_libraries(lc_runtime INTERFACE m) |
| 30 | + |
| 31 | +set(LC_BACKEND no CACHE STRING "Only compile the LC subset for the given backend") |
| 32 | +set(FAST no CACHE BOOL "Run supported tests with --fast") |
| 33 | + |
| 34 | +enable_testing() |
| 35 | + |
| 36 | +message("\n") |
| 37 | +message("Configuration results") |
| 38 | +message("---------------------") |
| 39 | +message("Fortran compiler: ${CMAKE_Fortran_COMPILER}") |
| 40 | +message("C compiler : ${CMAKE_C_COMPILER}") |
| 41 | +message("Build type: ${CMAKE_BUILD_TYPE}") |
| 42 | +if (CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 43 | + message("Fortran compiler flags: ${CMAKE_Fortran_FLAGS_DEBUG}") |
| 44 | + message("C compiler flags : ${CMAKE_C_FLAGS_DEBUG}") |
| 45 | +else () |
| 46 | + message("Fortran compiler flags: ${CMAKE_Fortran_FLAGS_RELEASE}") |
| 47 | + message("C compiler flags : ${CMAKE_C_FLAGS_RELEASE}") |
| 48 | +endif () |
| 49 | +message("Installation prefix: ${CMAKE_INSTALL_PREFIX}") |
| 50 | +message("LC_BACKEND: ${LC_BACKEND}") |
| 51 | +message("FAST: ${FAST}") |
| 52 | + |
| 53 | + |
| 54 | +macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN RUN_GCC_ARGS) |
| 55 | + set(fail ${${RUN_FAIL}}) |
| 56 | + set(name ${${RUN_NAME}}) |
| 57 | + set(file_name ${${RUN_FILE_NAME}}) |
| 58 | + set(labels ${${RUN_LABELS}}) |
| 59 | + set(extra_files ${${RUN_EXTRAFILES}}) |
| 60 | + set(extra_args ${${RUN_EXTRA_ARGS}}) |
| 61 | + set(copy_to_bin ${${RUN_COPY_TO_BIN}}) |
| 62 | + set(gcc_args ${${RUN_GCC_ARGS}}) |
| 63 | + |
| 64 | + if (NOT name) |
| 65 | + message(FATAL_ERROR "Must specify the NAME argument") |
| 66 | + endif() |
| 67 | + |
| 68 | + if (LC_BACKEND) |
| 69 | + if (${LC_BACKEND} IN_LIST labels) |
| 70 | + # Test is supported by the given LC backend |
| 71 | + set(ADD_TEST ON) |
| 72 | + else() |
| 73 | + # Test is not supported by the given LC backend |
| 74 | + set(ADD_TEST OFF) |
| 75 | + endif() |
| 76 | + else() |
| 77 | + # GCC |
| 78 | + if ("gcc" IN_LIST labels) |
| 79 | + set(ADD_TEST ON) |
| 80 | + else() |
| 81 | + set(ADD_TEST OFF) |
| 82 | + endif() |
| 83 | + endif() |
| 84 | + |
| 85 | + if (ADD_TEST) |
| 86 | + if ((LC_BACKEND STREQUAL "llvm") OR (LC_BACKEND STREQUAL "cpp") OR (LC_BACKEND STREQUAL "x86") |
| 87 | + OR (LC_BACKEND STREQUAL "c") OR (LC_BACKEND STREQUAL "fortran")) |
| 88 | + add_custom_command( |
| 89 | + OUTPUT ${name}.o |
| 90 | + COMMAND ${LC} -c ${extra_args} ${CMAKE_CURRENT_SOURCE_DIR}/${file_name} -o ${name}.o --extra-arg -I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include -- |
| 91 | + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file_name} |
| 92 | + VERBATIM) |
| 93 | + add_executable(${name} ${name}.o ${extra_files}) |
| 94 | + set_target_properties(${name} PROPERTIES LINKER_LANGUAGE C) |
| 95 | + target_link_libraries(${name} lc_runtime) |
| 96 | + add_test(${name} ${CURRENT_BINARY_DIR}/${name}) |
| 97 | + elseif (LC_BACKEND STREQUAL "wasm") |
| 98 | + # wasm test |
| 99 | + execute_process(COMMAND ${LC} ${extra_args} --backend=wasm ${CMAKE_CURRENT_SOURCE_DIR}/${file_name} -o ${name} |
| 100 | + --extra-arg -I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include --) |
| 101 | + find_program(WASM_EXEC_RUNTIME node) |
| 102 | + execute_process(COMMAND "${WASM_EXEC_RUNTIME}" --version |
| 103 | + OUTPUT_VARIABLE WASM_EXEC_VERSION |
| 104 | + OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 105 | + |
| 106 | + string(REGEX REPLACE "v([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1" NODE_MAJOR_VERSION "${WASM_EXEC_VERSION}") |
| 107 | + |
| 108 | + if (NODE_MAJOR_VERSION LESS 16) |
| 109 | + message(STATUS "${WASM_EXEC_RUNTIME} version: ${WASM_EXEC_VERSION}") |
| 110 | + set(WASM_EXEC_FLAGS "--experimental-wasm-bigint") |
| 111 | + endif() |
| 112 | + set(WASM_EXEC_FLAGS ${WASM_EXEC_FLAGS} "--experimental-wasi-unstable-preview1") |
| 113 | + add_test(${name} ${WASM_EXEC_RUNTIME} ${WASM_EXEC_FLAGS} ${CURRENT_BINARY_DIR}/${name}.js) |
| 114 | + else () |
| 115 | + add_executable(${name} ${file_name} ${extra_files}) |
| 116 | + target_compile_options(${name} PUBLIC ${gcc_args}) |
| 117 | + add_test(${name} ${CURRENT_BINARY_DIR}/${name}) |
| 118 | + endif() |
| 119 | + |
| 120 | + if (labels) |
| 121 | + set_tests_properties(${name} PROPERTIES LABELS "${labels}") |
| 122 | + endif() |
| 123 | + |
| 124 | + if (fail) |
| 125 | + set_tests_properties(${name} PROPERTIES WILL_FAIL TRUE) |
| 126 | + endif() |
| 127 | + |
| 128 | + if (copy_to_bin) |
| 129 | + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${copy_to_bin} DESTINATION ${CURRENT_BINARY_DIR}) |
| 130 | + endif() |
| 131 | + endif() |
| 132 | +endmacro(RUN_UTIL) |
| 133 | + |
| 134 | +macro(RUN) |
| 135 | + set(options FAIL NOFAST) |
| 136 | + set(oneValueArgs NAME INCLUDE_PATH COPY_TO_BIN) |
| 137 | + set(multiValueArgs LABELS EXTRAFILES EXTRA_ARGS GCC_ARGS) |
| 138 | + cmake_parse_arguments(RUN "${options}" "${oneValueArgs}" |
| 139 | + "${multiValueArgs}" ${ARGN} ) |
| 140 | + |
| 141 | + foreach(b ${RUN_LABELS}) |
| 142 | + if (NOT (b MATCHES "^(llvm|llvm2|llvm_rtlib|gcc|c|cpp|x86|wasm|gfortran|llvmImplicit|llvmStackArray|fortran|c_nopragma|llvm_nopragma)$")) |
| 143 | + message(FATAL_ERROR "Unsupported backend: ${b}") |
| 144 | + endif() |
| 145 | + endforeach() |
| 146 | + |
| 147 | + set(RUN_FILE_NAME ${RUN_NAME}) |
| 148 | + |
| 149 | + if (RUN_INCLUDE_PATH) |
| 150 | + # Only one include path supported for now |
| 151 | + # Later add support for multiple include paths by looping over and appending to extra args |
| 152 | + set(RUN_EXTRA_ARGS ${RUN_EXTRA_ARGS} -I${CMAKE_CURRENT_SOURCE_DIR}/${RUN_INCLUDE_PATH}) |
| 153 | + set(RUN_GCC_ARGS ${RUN_GCC_ARGS} -I${CMAKE_CURRENT_SOURCE_DIR}/${RUN_INCLUDE_PATH}) |
| 154 | + endif() |
| 155 | + |
| 156 | + if (NOT FAST) |
| 157 | + RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN RUN_GCC_ARGS) |
| 158 | + endif() |
| 159 | + |
| 160 | + if ((FAST) AND (NOT RUN_NOFAST)) |
| 161 | + set(RUN_EXTRA_ARGS ${RUN_EXTRA_ARGS} --fast) |
| 162 | + set(RUN_NAME "${RUN_NAME}_FAST") |
| 163 | + list(REMOVE_ITEM RUN_LABELS gfortran) # remove gfortran from --fast test |
| 164 | + RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN RUN_GCC_ARGS) |
| 165 | + endif() |
| 166 | +endmacro(RUN) |
| 167 | + |
| 168 | +# The supported test labels are: |
| 169 | +# |
| 170 | +# gcc --- compile with Gcc |
| 171 | +# fortran --- convert to Fortran code and compile with GFortran |
| 172 | +# llvm --- compile with LC, only generate binary code at the very end |
| 173 | +# llvm2 --- compile with LC, generate object files |
| 174 | +# llvm_rtlib --- compile with LC loading ASR runtime library, generate object files |
| 175 | +# cpp --- compile to C++, compile C++ to binary |
| 176 | +# x86 --- compile to x86 binary directly |
| 177 | +# wasm --- compile to WASM binary directly |
| 178 | + |
| 179 | +RUN(NAME expr1.c LABELS gcc c wasm llvm NOFAST) |
| 180 | + |
| 181 | +# arrays |
| 182 | +RUN(NAME array_01.cpp LABELS gcc llvm NOFAST) |
| 183 | +RUN(NAME array_02.cpp LABELS gcc llvm NOFAST) |
0 commit comments