|
| 1 | +# This file is part of the MicroPython project, http://micropython.org/ |
| 2 | +# |
| 3 | +# The MIT License (MIT) |
| 4 | +# |
| 5 | +# Copyright 2020 NXP |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +# THE SOFTWARE. |
| 24 | + |
1 | 25 | cmake_minimum_required(VERSION 3.13.1)
|
2 | 26 |
|
3 | 27 | find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
4 |
| -project(NONE) |
5 |
| - |
6 |
| -target_sources(app PRIVATE src/zephyr_start.c src/zephyr_getchar.c) |
| 28 | +project(micropython) |
7 | 29 |
|
8 |
| -add_library(libmicropython STATIC IMPORTED) |
9 |
| -set_target_properties(libmicropython PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libmicropython.a) |
10 |
| -target_link_libraries(app PUBLIC libmicropython) |
| 30 | +set(micropython_zephyr_dir ${CMAKE_CURRENT_SOURCE_DIR}) |
| 31 | +set(micropython_top_dir ${micropython_zephyr_dir}/../..) |
| 32 | +set(micropython_build_dir ${CMAKE_CURRENT_BINARY_DIR}/micropython) |
| 33 | +set(micropython_lib ${micropython_build_dir}/libmicropython.a) |
| 34 | +set(micropython_configport_h ${micropython_build_dir}/mpconfigport.h) |
11 | 35 |
|
| 36 | +# Get all the Zephyr include directories and cflags, and append them with what |
| 37 | +# we need to build MicroPython |
12 | 38 | zephyr_get_include_directories_for_lang_as_string(C includes)
|
13 | 39 | zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
|
14 | 40 | zephyr_get_compile_definitions_for_lang_as_string(C definitions)
|
15 | 41 | zephyr_get_compile_options_for_lang_as_string(C options)
|
16 | 42 |
|
17 |
| -add_custom_target( |
18 |
| - outputexports |
19 |
| - COMMAND echo CC="${CMAKE_C_COMPILER}" |
20 |
| - COMMAND echo AR="${CMAKE_AR}" |
21 |
| - COMMAND echo Z_CFLAGS=${system_includes} ${includes} ${definitions} ${options} |
22 |
| - VERBATIM |
23 |
| - USES_TERMINAL |
24 |
| -) |
| 43 | +set(micropython_cflags |
| 44 | +"${system_includes} ${includes} ${definitions} ${options} \ |
| 45 | +-I${micropython_build_dir} -I${micropython_zephyr_dir} -I${micropython_top_dir} \ |
| 46 | +-std=gnu99 -fomit-frame-pointer -DNDEBUG" |
| 47 | + ) |
| 48 | + |
| 49 | +# If we're calling make recursively, use the same make version as the top-level. |
| 50 | +if(CMAKE_GENERATOR STREQUAL "Unix Makefiles") |
| 51 | + set(submake "$(MAKE)") |
| 52 | +else() |
| 53 | + set(submake "make") |
| 54 | +endif() |
| 55 | + |
| 56 | +# Construct a list of Zephyr port sources, relative to the top-level |
| 57 | +# MicroPython directory. |
| 58 | +list(APPEND micropython_zephyr_src_list |
| 59 | + main.c help.c moduos.c modusocket.c modutime.c modzephyr.c modzsensor.c |
| 60 | + modmachine.c machine_i2c.c machine_pin.c uart_core.c zephyr_storage.c |
| 61 | + ) |
| 62 | +list(TRANSFORM micropython_zephyr_src_list PREPEND ports/zephyr/) |
| 63 | + |
| 64 | +# Construct a list of MicroPython lib sources used by the Zephyr port, relative |
| 65 | +# to the top-level MicroPython directory. |
| 66 | +list(APPEND micropython_lib_src_list |
| 67 | + timeutils/timeutils.c utils/mpirq.c utils/stdout_helpers.c utils/printf.c |
| 68 | + utils/pyexec.c utils/interrupt_char.c mp-readline/readline.c |
| 69 | + ) |
| 70 | +list(TRANSFORM micropython_lib_src_list PREPEND lib/) |
| 71 | + |
| 72 | +# Combine the lists of sources into one string and substitute it into a |
| 73 | +# generated Makefile in the build directory. The MicroPython lib target will |
| 74 | +# process them for QSTR generation and build them. |
| 75 | +list(APPEND micropython_src_list ${micropython_zephyr_src_list} ${micropython_lib_src_list}) |
| 76 | +list(JOIN micropython_src_list " \\\n\t" micropython_src) |
| 77 | +configure_file(${micropython_zephyr_dir}/Makefile.in ${micropython_build_dir}/Makefile @ONLY) |
| 78 | + |
| 79 | +# Copy the MicroPython configuration file set in Kconfig (usually |
| 80 | +# mpconfigport.h, but could also be mpconfigport_minimal.h or some other |
| 81 | +# user-defined file) into the build directory. This allows building |
| 82 | +# libmicropython.a outside of the Zephyr port source directory, which is useful |
| 83 | +# when building for multiple boards or regressing with the Zephyr sanitycheck |
| 84 | +# script. |
| 85 | +configure_file(${CONFIG_MICROPY_CONFIGFILE} ${micropython_configport_h} @ONLY) |
| 86 | + |
| 87 | +# Use the generated Makefile to build libmicropython.a |
| 88 | +include(ExternalProject) |
| 89 | +ExternalProject_Add( |
| 90 | + micropython_project |
| 91 | + PREFIX ${micropython_build_dir} |
| 92 | + SOURCE_DIR ${micropython_zephyr_dir} |
| 93 | + BINARY_DIR ${micropython_build_dir} |
| 94 | + CONFIGURE_COMMAND "" |
| 95 | + BUILD_COMMAND |
| 96 | + ${submake} |
| 97 | + CC=${CMAKE_C_COMPILER} |
| 98 | + AR=${CMAKE_AR} |
| 99 | + BUILD=${micropython_build_dir} |
| 100 | + MP_CONFIGFILE=<${micropython_configport_h}> |
| 101 | + FROZEN_DIR=${CONFIG_MICROPY_FROZEN_DIR} |
| 102 | + MICROPY_HEAP_SIZE=${CONFIG_MICROPY_HEAP_SIZE} |
| 103 | + MICROPY_VFS_FAT=$<BOOL:${CONFIG_MICROPY_VFS_FAT}> |
| 104 | + MICROPY_VFS_LFS1=$<BOOL:${CONFIG_MICROPY_VFS_LFS1}> |
| 105 | + MICROPY_VFS_LFS2=$<BOOL:${CONFIG_MICROPY_VFS_LFS2}> |
| 106 | + Z_CFLAGS=${micropython_cflags} |
| 107 | + DEPENDS zephyr_generated_headers |
| 108 | + INSTALL_COMMAND "" |
| 109 | + BUILD_BYPRODUCTS ${micropython_lib} |
| 110 | + ) |
| 111 | + |
| 112 | +# Create a wrapper CMake library that our application can link with |
| 113 | +add_library(micropython_lib STATIC IMPORTED GLOBAL) |
| 114 | +add_dependencies(micropython_lib micropython_project) |
| 115 | +set_target_properties(micropython_lib PROPERTIES IMPORTED_LOCATION ${micropython_lib}) |
| 116 | +set_target_properties(micropython_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${micropython_top_dir}) |
| 117 | +set_target_properties(micropython_lib PROPERTIES ADDITIONAL_CLEAN_FILES ${micropython_lib}) |
| 118 | + |
| 119 | +# Build the final application |
| 120 | +target_sources(app PRIVATE src/zephyr_start.c src/zephyr_getchar.c) |
| 121 | +target_link_libraries(app PUBLIC micropython_lib) |
0 commit comments