Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
cmake_minimum_required(VERSION 3.13)

# Save C2A_CORE_VERSION before project() command (project() can reset cache variables)
if(DEFINED C2A_CORE_VERSION)
set(_C2A_CORE_VERSION_SAVED "${C2A_CORE_VERSION}")
endif()

project(C2A_CORE)

# Restore C2A_CORE_VERSION after project()
if(DEFINED _C2A_CORE_VERSION_SAVED)
set(C2A_CORE_VERSION "${_C2A_CORE_VERSION_SAVED}" CACHE STRING "C2A Core version" FORCE)
unset(_C2A_CORE_VERSION_SAVED)
endif()

# Build option
## C2A compile config
option(C2A_BUILD_AS_UTF8 "Build C2A as UTF-8" ON)
Expand Down Expand Up @@ -33,6 +44,21 @@ option(C2A_USE_SIMPLE_LIBC "Use C2A-core hosted simple libc (c2a-core/l

set(C2A_CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

# C2A user ディレクトリの検出
# 優先順位: 1. CMake define 2. 環境変数 3. レガシーパス
if(NOT DEFINED C2A_USER_DIR)
if(DEFINED ENV{C2A_USER_DIR})
set(C2A_USER_DIR $ENV{C2A_USER_DIR})
message(STATUS "C2A user directory from environment: ${C2A_USER_DIR}")
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../src_user")
# レガシー git submodule 方式のフォールバック
set(C2A_USER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../src_user")
message(STATUS "C2A user directory (legacy fallback): ${C2A_USER_DIR}")
else()
message(WARNING "C2A_USER_DIR not set. Build may fail if src_user includes are used.")
endif()
endif()

set(C2A_SRCS
c2a_core_main.c
component_driver/cdrv_common_tlm_cmd_packet.c
Expand Down Expand Up @@ -78,13 +104,51 @@ execute_process(
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_REVISION_C2A_CORE
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_REVISION_C2A_CORE_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

# Git revision が取得できない場合のフォールバック(crates.io からのダウンロード時など)
if(NOT GIT_REVISION_C2A_CORE)
# C2A_CORE_VERSION が指定されている場合は crate version を使用
if(DEFINED C2A_CORE_VERSION)
set(GIT_REVISION_C2A_CORE "v${C2A_CORE_VERSION}")
message(STATUS "Using crate version as revision: v${C2A_CORE_VERSION}")
else()
set(GIT_REVISION_C2A_CORE "unknown")
message(WARNING "Git revision not available (not a git repository)")
endif()
endif()
if(NOT GIT_REVISION_C2A_CORE_SHORT)
# C2A_CORE_VERSION が指定されている場合は version から数値を生成
if(DEFINED C2A_CORE_VERSION)
# バージョンを 0xMMmmpp 形式に変換 (例: 4.5.1 -> 0x00040501)
string(REPLACE "." ";" VERSION_LIST ${C2A_CORE_VERSION})
list(GET VERSION_LIST 0 VERSION_MAJOR)
list(GET VERSION_LIST 1 VERSION_MINOR)
list(LENGTH VERSION_LIST VERSION_LIST_LEN)
if(VERSION_LIST_LEN GREATER 2)
list(GET VERSION_LIST 2 VERSION_PATCH)
else()
set(VERSION_PATCH 0)
endif()
math(EXPR VERSION_HEX "(${VERSION_MAJOR} << 16) + (${VERSION_MINOR} << 8) + ${VERSION_PATCH}" OUTPUT_FORMAT HEXADECIMAL)
# Remove 0x prefix since add_definitions will add it
string(SUBSTRING "${VERSION_HEX}" 2 -1 VERSION_HEX_NO_PREFIX)
set(GIT_REVISION_C2A_CORE_SHORT "${VERSION_HEX_NO_PREFIX}")
message(STATUS "Using crate version as short revision: 0x${VERSION_HEX_NO_PREFIX}")
else()
set(GIT_REVISION_C2A_CORE_SHORT "00000000")
message(WARNING "Git short revision not available (not a git repository)")
endif()
endif()

add_definitions("-DGIT_REVISION_C2A_CORE=\"${GIT_REVISION_C2A_CORE}\"")
add_definitions("-DGIT_REVISION_C2A_CORE_SHORT=0x${GIT_REVISION_C2A_CORE_SHORT}")

Expand All @@ -94,6 +158,22 @@ endif()

add_library(${PROJECT_NAME} OBJECT ${C2A_SRCS})

# C2A user の親ディレクトリをインクルードパスに追加
# これにより <src_user/...> が解決可能になる(レガシー方式用)
if(DEFINED C2A_USER_DIR AND EXISTS "${C2A_USER_DIR}")
get_filename_component(C2A_USER_PARENT_DIR "${C2A_USER_DIR}" DIRECTORY)
target_include_directories(${PROJECT_NAME} PUBLIC "${C2A_USER_PARENT_DIR}")
message(STATUS "C2A user include path: ${C2A_USER_PARENT_DIR}")
endif()

# ビルドディレクトリ内の src_user シンボリックリンクを解決するため、
# C2A_BUILD_OUT_DIR が指定されている場合は include path に追加
# これにより実際のディレクトリ名を src_user に限定する必要がなくなる
if(DEFINED C2A_BUILD_OUT_DIR)
target_include_directories(${PROJECT_NAME} PUBLIC "${C2A_BUILD_OUT_DIR}")
message(STATUS "C2A build output directory: ${C2A_BUILD_OUT_DIR}")
endif()

if(C2A_USE_ALL_CORE_APPS)
add_subdirectory(applications)
target_sources(${PROJECT_NAME} PUBLIC $<TARGET_OBJECTS:C2A_CORE_APPS>)
Expand Down
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ members = [
"./examples/subobc",
]

exclude = [
"./examples/tmp", # テスト用
]

[workspace.dependencies]
c2a-core = { path = "." }
c2a-core = { path = ".", version = "4.5.1" }
c2a-bind-utils = { path = "./library/bind-utils" }

c2a-dev-runtime = { path = "./dev-runtime" }
Expand Down Expand Up @@ -58,4 +62,4 @@ c2a-bind-utils = "4.0.0-beta.0"

[dev-dependencies]
# unit test 時に libC2A.a を要求しないようにする
c2a-core = { path = ".", features = ["no-c2a-link"] }
c2a-core = { path = ".", version = "4.5.1", features = ["no-c2a-link"] }
86 changes: 43 additions & 43 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ fn main() {
println!("cargo:return-if-changed=tlm_cmd/");

let ver = env!("CARGO_PKG_VERSION");
// crate version を依存 crate に export(git revision の代替として使用)
println!("cargo:VERSION={ver}");

let ver = Version::parse(ver).unwrap();
dbg!(&ver);

Expand Down Expand Up @@ -112,51 +115,48 @@ fn get_definitions(src_file: &str) -> HashMap<String, Option<String>> {
let childlen = entity.get_children().into_iter();

for cursor in childlen {
match cursor.get_kind() {
clang::EntityKind::MacroDefinition => {
let location = cursor.get_location().unwrap().get_file_location();
if let Some(file) = location.file {
let file = file.get_path();
let _f = file.to_str().unwrap();
} else {
continue;
}

let name = cursor.get_display_name().unwrap();
let mut token = cursor.get_range().unwrap().tokenize();
token.remove(0); // remove macro Identifier token
if token.is_empty() {
macros.insert(name, None);
continue; // remove define only
}

let first = token.first().unwrap();
let last = token.last().unwrap();
if first.get_kind() == Punctuation
&& last.get_kind() == Punctuation
&& first.get_spelling() == "("
&& last.get_spelling() == ")"
{
token.remove(0);
token.remove(token.len() - 1);
}

if token.len() == 1 {
let value = token[0].get_spelling();

let value = if value.starts_with('\"') && value.ends_with('\"') {
let value = value.strip_prefix('\"').unwrap();
value.strip_suffix('\"').unwrap().to_string()
} else {
value
};
macros.insert(name, Some(value));
if cursor.get_kind() == clang::EntityKind::MacroDefinition {
let location = cursor.get_location().unwrap().get_file_location();
if let Some(file) = location.file {
let file = file.get_path();
let _f = file.to_str().unwrap();
} else {
continue;
}

let name = cursor.get_display_name().unwrap();
let mut token = cursor.get_range().unwrap().tokenize();
token.remove(0); // remove macro Identifier token
if token.is_empty() {
macros.insert(name, None);
continue; // remove define only
}

let first = token.first().unwrap();
let last = token.last().unwrap();
if first.get_kind() == Punctuation
&& last.get_kind() == Punctuation
&& first.get_spelling() == "("
&& last.get_spelling() == ")"
{
token.remove(0);
token.remove(token.len() - 1);
}

if token.len() == 1 {
let value = token[0].get_spelling();

let value = if value.starts_with('\"') && value.ends_with('\"') {
let value = value.strip_prefix('\"').unwrap();
value.strip_suffix('\"').unwrap().to_string()
} else {
// 単純な値ではなかった(ex: 関数マクロ)
dbg!(token);
}
value
};
macros.insert(name, Some(value));
} else {
// 単純な値ではなかった(ex: 関数マクロ)
dbg!(token);
}
_ => {}
}
}

Expand Down
2 changes: 2 additions & 0 deletions examples/mobc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/compile_commands.json
/.vscode/c_cpp_properties.json
38 changes: 34 additions & 4 deletions examples/mobc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,39 @@ if(C2A_BUILD_AS_CXX)
message("Build C2A as C++!")
endif()

set(C2A_CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/src_core)
set(C2A_USER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/src_user)
# C2A_CORE_DIR と C2A_USER_DIR の設定
# 優先順位: 1. CMake define (-D で指定) 2. レガシーパス(シンボリックリンク)
if(NOT DEFINED C2A_CORE_DIR)
set(C2A_CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/src_core)
message(STATUS "C2A_CORE_DIR (legacy): ${C2A_CORE_DIR}")
else()
message(STATUS "C2A_CORE_DIR (defined): ${C2A_CORE_DIR}")
endif()

if(NOT DEFINED C2A_USER_DIR)
set(C2A_USER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/src_user)
message(STATUS "C2A_USER_DIR (legacy): ${C2A_USER_DIR}")
else()
message(STATUS "C2A_USER_DIR (defined): ${C2A_USER_DIR}")
endif()

include_directories(src)
# C2A user の親ディレクトリをインクルードパスに追加
get_filename_component(C2A_USER_PARENT_DIR "${C2A_USER_DIR}" DIRECTORY)
include_directories("${C2A_USER_PARENT_DIR}")

# C2A core の親ディレクトリもインクルードパスに追加(<src_core/...> 用)
get_filename_component(C2A_CORE_PARENT_DIR "${C2A_CORE_DIR}" DIRECTORY)
if(NOT "${C2A_CORE_PARENT_DIR}" STREQUAL "${C2A_USER_PARENT_DIR}")
include_directories("${C2A_CORE_PARENT_DIR}")
endif()

# ビルドディレクトリ内の src_core と src_user シンボリックリンクを解決するため、
# C2A_BUILD_OUT_DIR が指定されている場合は include path に追加
# これにより実際のディレクトリ名を src_core/src_user に限定する必要がなくなる
if(DEFINED C2A_BUILD_OUT_DIR)
include_directories("${C2A_BUILD_OUT_DIR}")
message(STATUS "C2A build output directory: ${C2A_BUILD_OUT_DIR}")
endif()

# Output debug print to SILS console window
option(C2A_SHOW_DEBUG_PRINT_ON_SILS "Show debug print" ON)
Expand All @@ -62,7 +91,8 @@ execute_process(
add_definitions("-DGIT_REVISION_C2A_USER=\"${GIT_REVISION_C2A_USER}\"")
add_definitions("-DGIT_REVISION_C2A_USER_SHORT=0x${GIT_REVISION_C2A_USER_SHORT}")

add_subdirectory(${C2A_CORE_DIR})
# out-of-tree source の場合、binary directory を明示的に指定
add_subdirectory(${C2A_CORE_DIR} ${CMAKE_BINARY_DIR}/c2a_core)

add_subdirectory(${C2A_USER_DIR}/applications)
add_subdirectory(${C2A_USER_DIR}/component_driver)
Expand Down
12 changes: 11 additions & 1 deletion examples/mobc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version.workspace = true
edition = "2021"

[dependencies]
c2a-core.workspace = true
# export-src は build.rs で source_dir() を使うため常に必要
c2a-core = { workspace = true, features = ["export-src"] }

# runtime
c2a-dev-runtime.workspace = true
Expand All @@ -16,3 +17,12 @@ c2a-wdt-noop.workspace = true

[build-dependencies]
cmake = "0.1"
serde_json = "1.0"
# build.rs では source_dir() を呼ぶだけなので、no-c2a-link で C ライブラリをリンクしない
c2a-core = { workspace = true, features = ["export-src", "no-c2a-link"] }

[features]
default = []
# ローカル開発用: シンボリックリンク方式を使う
# cargo build --features use-local-c2a-core
use-local-c2a-core = []
Loading
Loading