|
| 1 | +cmake_minimum_required(VERSION 3.13) |
| 2 | + |
| 3 | +if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}") |
| 4 | + message(FATAL_ERROR "In-source builds are not allowed. |
| 5 | + Please create a build directory and use `cmake ..` inside it. |
| 6 | + NOTE: cmake will now create CMakeCache.txt and CMakeFiles/*. |
| 7 | + You must delete them, or cmake will refuse to work.") |
| 8 | +endif() |
| 9 | + |
| 10 | +project(note_c) |
| 11 | + |
| 12 | +# Automatically ignore CMake build directory. |
| 13 | +if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore) |
| 14 | + file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*") |
| 15 | +endif() |
| 16 | + |
| 17 | +option(BUILD_TESTS "Build tests." ON) |
| 18 | +option(BUILD_SHARED_LIBS "Build note-c as a shared library." ON) |
| 19 | +option(COVERAGE "Compile for test coverage reporting." OFF) |
| 20 | + |
| 21 | +add_compile_options( |
| 22 | + -Wall |
| 23 | + -Wextra |
| 24 | + -Wpedantic |
| 25 | + -Werror |
| 26 | +) |
| 27 | + |
| 28 | +set(NOTE_C_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 29 | +add_library( |
| 30 | + note_c |
| 31 | + ${NOTE_C_SRC_DIR}/n_atof.c |
| 32 | + ${NOTE_C_SRC_DIR}/n_cjson.c |
| 33 | + ${NOTE_C_SRC_DIR}/n_const.c |
| 34 | + ${NOTE_C_SRC_DIR}/n_helpers.c |
| 35 | + ${NOTE_C_SRC_DIR}/n_i2c.c |
| 36 | + ${NOTE_C_SRC_DIR}/n_printf.c |
| 37 | + ${NOTE_C_SRC_DIR}/n_serial.c |
| 38 | + ${NOTE_C_SRC_DIR}/n_ua.c |
| 39 | + ${NOTE_C_SRC_DIR}/n_b64.c |
| 40 | + ${NOTE_C_SRC_DIR}/n_cjson_helpers.c |
| 41 | + ${NOTE_C_SRC_DIR}/n_ftoa.c |
| 42 | + ${NOTE_C_SRC_DIR}/n_hooks.c |
| 43 | + ${NOTE_C_SRC_DIR}/n_md5.c |
| 44 | + ${NOTE_C_SRC_DIR}/n_request.c |
| 45 | + ${NOTE_C_SRC_DIR}/n_str.c |
| 46 | +) |
| 47 | + |
| 48 | +if(BUILD_TESTS) |
| 49 | + add_compile_definitions( |
| 50 | + TEST |
| 51 | + ) |
| 52 | + |
| 53 | + # Including this here rather than in test/CMakeLists.txt allows us to run |
| 54 | + # ctest from the root build directory (e.g. build/ instead of build/test/). |
| 55 | + include(CTest) |
| 56 | + |
| 57 | + # If we don't weaken the functions we're mocking in the tests, the linker |
| 58 | + # will complain about multiple function definitions: the mocked one and the |
| 59 | + # "real" one from note-c. Weakening the real function causes the mock |
| 60 | + # function, if defined, to override the real one. If no mock is defined, the |
| 61 | + # real one will be used. So, every time a developer needs to mock a function |
| 62 | + # in a test, they need to make sure it's included in the MOCKED_FNS list |
| 63 | + # below. |
| 64 | + set( |
| 65 | + MOCKED_FNS |
| 66 | + "NoteReset; |
| 67 | + NoteJSONTransaction; |
| 68 | + NoteTransaction; |
| 69 | + NoteGetMs; |
| 70 | + NoteRequestResponse; |
| 71 | + NoteMalloc; |
| 72 | + NoteI2CTransmit; |
| 73 | + NoteI2CReceive; |
| 74 | + NoteLockI2C; |
| 75 | + NoteUnlockI2C; |
| 76 | + NoteI2CReset; |
| 77 | + NoteSerialAvailable; |
| 78 | + NoteSerialTransmit; |
| 79 | + NoteSerialReceive; |
| 80 | + NoteSerialReset; |
| 81 | + NoteIsDebugOutputActive; |
| 82 | + NoteDebug; |
| 83 | + NotePrint; |
| 84 | + NoteNewCommand; |
| 85 | + NoteRequest" |
| 86 | + ) |
| 87 | + foreach(MOCKED_FN ${MOCKED_FNS}) |
| 88 | + string(APPEND OBJCOPY_WEAKEN "-W ${MOCKED_FN} ") |
| 89 | + endforeach() |
| 90 | + separate_arguments(OBJCOPY_WEAKEN_LIST NATIVE_COMMAND "${OBJCOPY_WEAKEN}") |
| 91 | + add_custom_command(TARGET note_c POST_BUILD |
| 92 | + COMMAND ${CMAKE_OBJCOPY} ${OBJCOPY_WEAKEN_LIST} |
| 93 | + $<TARGET_FILE:note_c> |
| 94 | + COMMENT "Weakening mocked functions." |
| 95 | + ) |
| 96 | + |
| 97 | + add_subdirectory(test) |
| 98 | +endif(BUILD_TESTS) |
0 commit comments