nuttx/cmake/nuttx_mkversion.cmake
chao an 6ee9ec7656 build: add initial cmake build system
1. Update all CMakeLists.txt to adapt to new layout
2. Fix cmake build break
3. Update all new file license
4. Fully compatible with current compilation environment(use configure.sh or cmake as you choose)

------------------

How to test

From within nuttx/. Configure:

cmake -B build -DBOARD_CONFIG=sim/nsh -GNinja
cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja

(or full path in custom board) :
cmake -B build -DBOARD_CONFIG=$PWD/boards/sim/sim/sim/configs/nsh -GNinja

This uses ninja generator (install with sudo apt install ninja-build). To build:

$ cmake --build build

menuconfig:

$ cmake --build build -t menuconfig

--------------------------

2. cmake/build: reformat the cmake style by cmake-format

https://github.com/cheshirekow/cmake_format

$ pip install cmakelang

$ for i in `find -name CMakeLists.txt`;do cmake-format $i -o $i;done
$ for i in `find -name *\.cmake`;do cmake-format $i -o $i;done

Co-authored-by: Matias N <matias@protobits.dev>
Signed-off-by: chao an <anchao@xiaomi.com>
2023-07-08 13:50:48 +08:00

111 lines
3.9 KiB
CMake

# ##############################################################################
# cmake/nuttx_mkversion.cmake
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################
set(VERSION_H ${CMAKE_BINARY_DIR}/include/nuttx/version.h)
if(EXISTS ${VERSION_H})
return()
endif()
execute_process(
COMMAND git -C ${NUTTX_DIR} describe --match "nuttx-*"
WORKING_DIRECTORY ${NUTTX_DIR}
OUTPUT_VARIABLE NUTTX_VERSION
RESULT_VARIABLE VERSION_STATUS)
if(${VERSION_STATUS} AND NOT ${VERSION_STATUS} EQUAL 0)
set(NUTTX_VERSION "0.0.0")
else()
string(REPLACE "-" ";" NUTTX_VERSION ${NUTTX_VERSION})
list(GET NUTTX_VERSION 1 NUTTX_VERSION)
endif()
string(REPLACE "." ";" NUTTX_CHECK_VERSION ${NUTTX_VERSION})
list(LENGTH NUTTX_CHECK_VERSION NUTTX_VERSION_LENGTH)
if(${NUTTX_VERSION_LENGTH} LESS 3)
execute_process(
COMMAND git -C ${NUTTX_DIR} -c "versionsort.suffix=-" tag --sort=v:refname
WORKING_DIRECTORY ${NUTTX_DIR}
OUTPUT_VARIABLE NUTTX_VERSION_LIST
RESULT_VARIABLE VERSION_STATUS)
if(${VERSION_STATUS} EQUAL 0)
string(REPLACE "\n" ";" NUTTX_VERSION_LIST ${NUTTX_VERSION_LIST})
foreach(version ${NUTTX_VERSION_LIST})
string(REGEX MATCH "nuttx-[0-9]+\.[0-9]+\.[0-9]+" NUTTX_CHECK_VERSION
${version})
if(NUTTX_CHECK_VERSION)
string(REPLACE "-" ";" NUTTX_VERSION ${NUTTX_CHECK_VERSION})
list(GET NUTTX_VERSION 1 NUTTX_VERSION)
endif()
endforeach()
endif()
endif()
execute_process(
COMMAND git -C ${NUTTX_DIR} log --oneline -1
WORKING_DIRECTORY ${NUTTX_DIR}
OUTPUT_VARIABLE NUTTX_VERSION_BUILD
RESULT_VARIABLE VERSION_STATUS)
if(${VERSION_STATUS} AND NOT ${VERSION_STATUS} EQUAL 0)
set(NUTTX_VERSION_BUILD)
else()
string(REPLACE " " ";" NUTTX_VERSION_BUILD ${NUTTX_VERSION_BUILD})
list(GET NUTTX_VERSION_BUILD 0 NUTTX_VERSION_BUILD)
execute_process(
COMMAND git -C ${NUTTX_DIR} diff-index --name-only HEAD
WORKING_DIRECTORY ${NUTTX_DIR}
OUTPUT_VARIABLE NUTTX_VERSION_BUILD_DIRTY)
if(NUTTX_VERSION_BUILD_DIRTY)
set(NUTTX_VERSION_BUILD ${NUTTX_VERSION_BUILD}-dirty)
endif()
endif()
file(WRITE ${VERSION_H} "/* version.h -- Autogenerated! Do not edit. */\n\n")
file(APPEND ${VERSION_H} "#ifndef __INCLUDE_NUTTX_VERSION_H\n")
file(APPEND ${VERSION_H} "#define __INCLUDE_NUTTX_VERSION_H\n\n")
file(APPEND ${VERSION_H} "#define CONFIG_VERSION_STRING \"${NUTTX_VERSION}\"\n")
string(REPLACE "." ";" NUTTX_VERSION ${NUTTX_VERSION})
list(GET NUTTX_VERSION 0 NUTTX_VERSION_MAJOR)
list(GET NUTTX_VERSION 1 NUTTX_VERSION_MINOR)
list(GET NUTTX_VERSION 2 NUTTX_VERSION_PATCH)
file(APPEND ${VERSION_H}
"#define CONFIG_VERSION_MAJOR ${NUTTX_VERSION_MAJOR}\n")
file(APPEND ${VERSION_H}
"#define CONFIG_VERSION_MINOR ${NUTTX_VERSION_MINOR}\n")
file(APPEND ${VERSION_H}
"#define CONFIG_VERSION_PATCH ${NUTTX_VERSION_PATCH}\n")
file(APPEND ${VERSION_H}
"#define CONFIG_VERSION_BUILD \"${NUTTX_VERSION_BUILD}\"\n\n")
file(
APPEND ${VERSION_H}
"#define CONFIG_VERSION ((CONFIG_VERSION_MAJOR << 16) | \\
(CONFIG_VERSION_MINOR << 8) | \\
(CONFIG_VERSION_PATCH))\n")
file(APPEND ${VERSION_H} "\n#endif /* __INCLUDE_NUTTX_VERSION_H */\n")