aboutsummaryrefslogtreecommitdiff
path: root/CMakeLists.txt
blob: 37a4912ddcccaaa721326a8ff63d83191abf0ebe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# nanowasm, a tiny WebAssembly/Wasm interpreter
# Copyright (C) 2023-2025  Xavier Del Campo Romero
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

cmake_minimum_required(VERSION 3.19)
project(nanowasm LANGUAGES C VERSION 0.0.0)
option(NW_EXAMPLES "Build examples")
option(NW_LOG "Enables logging to stderr")
option(NW_LOG_CUSTOM "Allows user code to define nwp_log. Enables NW_LOG")
option(NW_CHECK_CODE "Check opcodes inside code section (slow).")
add_library(${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} PUBLIC include
    PRIVATE private_include)

set(compilers
    "GNU"
    "Clang"
    "TinyCC"
)

if(NW_LOG_CUSTOM)
    set(NW_LOG ON)
endif()

foreach(c ${compilers})
    if(CMAKE_C_COMPILER_ID STREQUAL ${c})
        set(cflags_np
            -pedantic
            -Wall
        )

        if(CMAKE_BUILD_TYPE STREQUAL "Debug")
            set(cflags_np ${cflags_np} -Og -g)
        else()
            set(cflags_np ${cflags_np} -Os)
        endif()

        break()
    endif()
endforeach()

include(CheckCompilerFlag)

foreach(f ${cflags_np})
    string(REPLACE "-" "_" var supported_${f})
    check_compiler_flag(C ${f} ${var})

    if(${var})
        set(sup_cflags ${sup_cflags} ${f})
    endif()
endforeach()

if(NW_LOG)
    target_compile_definitions(${PROJECT_NAME} PRIVATE NW_LOG)
endif()

if(NW_EXAMPLES)
    add_subdirectory(examples)
endif()

target_compile_options(${PROJECT_NAME} PRIVATE ${sup_cflags})
set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 90 C_EXTENSIONS OFF)
add_subdirectory(src)
install(TARGETS ${PROJECT_NAME})