-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from Anthony-Dong/dev
Dev
- Loading branch information
Showing
15 changed files
with
557 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cmake | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/anthony-dong/golang/command" | ||
"github.com/anthony-dong/golang/command/cmake/static" | ||
"github.com/anthony-dong/golang/pkg/logs" | ||
"github.com/anthony-dong/golang/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommand() (*cobra.Command, error) { | ||
cmd := &cobra.Command{ | ||
Use: "cmake", | ||
Short: "cmake tools", | ||
Example: ` | ||
1. devtool cmake init --name xxx | ||
2. cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE -S . -B output | ||
3. cmake --build output --config Debug --target main -j 8 | ||
4. ./output/main | ||
`, | ||
} | ||
if err := command.AddCommand(cmd, NewInitCommand); err != nil { | ||
return nil, err | ||
} | ||
return cmd, nil | ||
} | ||
|
||
func NewInitCommand() (*cobra.Command, error) { | ||
name := "" | ||
dir := "" | ||
cmd := &cobra.Command{ | ||
Use: "init", | ||
Short: "init cmake project", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
files, err := static.ReadAllFiles() | ||
if err != nil { | ||
return err | ||
} | ||
for file, content := range files { | ||
if file == static.CMakeListsFile { | ||
content = strings.ReplaceAll(content, "PROJECT_NAME", name) | ||
} | ||
if err := utils.WriteFileForce(filepath.Join(dir, file), []byte(content)); err != nil { | ||
return err | ||
} | ||
} | ||
logs.Info("init cmake project success.") | ||
logs.Info("step1(config): cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE -S . -B output") | ||
logs.Info("step2(build): cmake --build output --config Debug --target main -j 8") | ||
logs.Info("step3(run): ./output/main") | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&name, "name", "N", "", "the project name") | ||
if err := cmd.MarkFlagRequired("name"); err != nil { | ||
return nil, err | ||
} | ||
cmd.Flags().StringVarP(&dir, "dir", "D", utils.GetPwd(), "the project dir") | ||
return cmd, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
project(PROJECT_NAME) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) # force stdc++=20 | ||
set(CMAKE_CXX_EXTENSIONS OFF) # 禁止使用GUN特性 | ||
|
||
include (cmake/cc_library.cmake) | ||
include (cmake/cc_binary.cmake) | ||
include (cmake/cc_test.cmake) | ||
|
||
cc_binary( | ||
NAME main | ||
SRCS main.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# Bazel Rule | ||
|
||
bazel 的规则定义语法实际上非常优与 cmake,cmake 会显得非常的丑陋且非常臃肿,因此很多开源项目都封装了 cmake rule,方便进行依赖管理. | ||
|
||
# 快速开始 | ||
|
||
```cmake | ||
# 添加依赖 | ||
include (cmake/cc_library.cmake) | ||
include (cmake/cc_binary.cmake) | ||
include (cmake/cc_test.cmake) | ||
# 设置项目src路径,方便项目下别的文件include | ||
list (APPEND CUSTOM_PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) | ||
``` | ||
|
||
# cc_library | ||
|
||
1. [bazel](https://bazel.build/reference/be/c-cpp#cc_library) | ||
|
||
```build | ||
load("@rules_cc//cc:defs.bzl", "cc_library") | ||
cc_library( | ||
name = "times", | ||
srcs = [ | ||
"times.cpp", | ||
], | ||
hdrs = ["times.h"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//utils:times", | ||
"@com_google_absl//absl/strings", | ||
"@com_google_absl//absl/strings:str_format", | ||
], | ||
) | ||
``` | ||
|
||
2. [cmake](cc_library.cmake) | ||
|
||
```cmake | ||
cc_library ( | ||
NAME cpp_network | ||
ALIAS cpp::network | ||
SRCS listener.cpp | ||
HDRS header.h utils.h task_queue.h listener.h | ||
DEPS event cpp::utils | ||
) | ||
``` | ||
|
||
# cc_binary | ||
|
||
1. [bazel](https://bazel.build/reference/be/c-cpp#cc_binary) | ||
|
||
```build | ||
load("@rules_cc//cc:defs.bzl", "cc_binary") # 高版本的bazel不需要load cc_binary | ||
cc_binary( | ||
name = "main", | ||
srcs = [ | ||
"main.cpp", | ||
], | ||
deps = [ | ||
"//utils:times", | ||
"@com_google_absl//absl/strings", | ||
"@com_google_absl//absl/strings:str_format", | ||
], | ||
) | ||
``` | ||
|
||
2. [cmake](cc_binary.cmake) | ||
|
||
```cmake | ||
cc_binary ( | ||
NAME network_nio_main | ||
SRCS network/nio_main.cpp | ||
DEPS cpp::network cpp::log | ||
) | ||
``` | ||
|
||
# cc_test | ||
|
||
1. [bazel](https://bazel.build/reference/be/c-cpp#cc_test) | ||
|
||
> https://google.github.io/googletest/quickstart-bazel.html | ||
```build | ||
cc_test( | ||
name = "hello_test", | ||
size = "small", | ||
srcs = ["hello_test.cc"], | ||
deps = ["@com_google_googletest//:gtest_main"], | ||
) | ||
``` | ||
|
||
2. [cmake](cc_test.cmake) | ||
|
||
> https://google.github.io/googletest/quickstart-cmake.html | ||
```cmake | ||
include(FetchContent) | ||
FetchContent_Declare( | ||
googletest | ||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip | ||
) | ||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
FetchContent_MakeAvailable(googletest) | ||
cc_test ( | ||
NAME utils_time_test | ||
SRCS utils/time_test.cpp | ||
DEPS cpp::utils | ||
) | ||
``` | ||
|
||
# TODO | ||
|
||
- 生成`pkgconfig`文件 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 参考自 https://github.com/google/ruy/tree/master/cmake | ||
|
||
include(CMakeParseArguments) | ||
|
||
# cc_binary() | ||
# | ||
# CMake function to imitate Bazel's cc_binary rule. | ||
# CUSTOM_PROJECT_SOURCE_DIR: 业务自定义的项目根路径 | ||
function(cc_binary) | ||
cmake_parse_arguments( | ||
_RULE | ||
"" | ||
"NAME" | ||
"SRCS;COPTS;LINKOPTS;DEPS;INCLUDE_DIRS" | ||
${ARGN} | ||
) | ||
|
||
if (DEFINED CUSTOM_PROJECT_SOURCE_DIR) | ||
list(APPEND _RULE_INCLUDE_DIRS ${CUSTOM_PROJECT_SOURCE_DIR}) | ||
endif () | ||
|
||
message("cc_binary( | ||
# pwd: ${CMAKE_CURRENT_LIST_DIR} | ||
name: ${_RULE_NAME}, | ||
srcs: ${_RULE_SRCS}, | ||
deps: ${_RULE_DEPS}, | ||
includes: ${_RULE_INCLUDE_DIRS}, | ||
)") | ||
add_executable(${_RULE_NAME} "") | ||
target_sources(${_RULE_NAME} | ||
PRIVATE | ||
${_RULE_SRCS} | ||
) | ||
set_target_properties(${_RULE_NAME} PROPERTIES OUTPUT_NAME "${_RULE_NAME}") | ||
target_include_directories(${_RULE_NAME} | ||
PUBLIC | ||
"$<BUILD_INTERFACE:${_RULE_INCLUDE_DIRS}>" # CMAKE_SOURCE_DIR | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" | ||
) | ||
target_compile_options(${_RULE_NAME} | ||
PRIVATE | ||
${_RULE_COPTS} | ||
) | ||
target_link_options(${_RULE_NAME} | ||
PRIVATE | ||
${_RULE_LINKOPTS} | ||
) | ||
target_link_libraries(${_RULE_NAME} | ||
PUBLIC | ||
${_RULE_DEPS} | ||
) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# 参考自 https://github.com/google/ruy/tree/master/cmake | ||
|
||
include(CMakeParseArguments) | ||
|
||
# cc_library() | ||
# | ||
# CMake function to imitate Bazel's cc_library rule. | ||
# CUSTOM_PROJECT_SOURCE_DIR: 业务自定义项目根路径 | ||
function(cc_library) | ||
cmake_parse_arguments( | ||
_RULE | ||
"TESTONLY" | ||
"NAME;ALIAS" | ||
"HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS;INCLUDE_DIRS" | ||
${ARGN} | ||
) | ||
|
||
# Check if this is a header-only library. | ||
if("${_RULE_SRCS}" STREQUAL "") | ||
set(_RULE_IS_INTERFACE 1) | ||
else() | ||
set(_RULE_IS_INTERFACE 0) | ||
endif() | ||
|
||
if (DEFINED CUSTOM_PROJECT_SOURCE_DIR) | ||
list(APPEND _RULE_INCLUDE_DIRS ${CUSTOM_PROJECT_SOURCE_DIR}) | ||
endif () | ||
|
||
file(RELATIVE_PATH _RULE_SUBDIR ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_LIST_DIR}) | ||
|
||
message("cc_library( | ||
# pwd: ${CMAKE_CURRENT_LIST_DIR} | ||
name: ${_RULE_NAME}, | ||
alias: ${_RULE_ALIAS}, | ||
srcs: ${_RULE_SRCS}, | ||
hdrs: ${_RULE_HDRS}, | ||
deps: ${_RULE_DEPS}, | ||
includes: ${_RULE_INCLUDE_DIRS}, | ||
)") | ||
|
||
if(_RULE_IS_INTERFACE) | ||
# Generating a header-only library. | ||
add_library(${_RULE_NAME} INTERFACE) | ||
set_target_properties(${_RULE_NAME} PROPERTIES PUBLIC_HEADER "${_RULE_HDRS}") | ||
target_include_directories(${_RULE_NAME} | ||
INTERFACE | ||
"$<BUILD_INTERFACE:${_RULE_INCLUDE_DIRS}>" # 指定include路径 | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" # 指定install路径 | ||
) | ||
target_link_libraries(${_RULE_NAME} | ||
INTERFACE | ||
${_RULE_DEPS} | ||
${_RULE_LINKOPTS} | ||
) | ||
target_compile_definitions(${_RULE_NAME} | ||
INTERFACE | ||
${_RULE_DEFINES} | ||
) | ||
else() | ||
# Generating a static binary library. | ||
add_library(${_RULE_NAME} STATIC ${_RULE_SRCS} ${_RULE_HDRS}) | ||
set_target_properties(${_RULE_NAME} PROPERTIES PUBLIC_HEADER "${_RULE_HDRS}") | ||
target_include_directories(${_RULE_NAME} | ||
PUBLIC | ||
"$<BUILD_INTERFACE:${CPP_COMMON_INCLUDE_DIRS}>" # CMAKE_SOURCE_DIR/PROJECT_SOURCE_DIR | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" | ||
) | ||
target_compile_options(${_RULE_NAME} | ||
PRIVATE | ||
${_RULE_COPTS} | ||
) | ||
target_link_libraries(${_RULE_NAME} | ||
PUBLIC | ||
${_RULE_DEPS} | ||
PRIVATE | ||
${_RULE_LINKOPTS} | ||
) | ||
target_compile_definitions(${_RULE_NAME} | ||
PUBLIC | ||
${_RULE_DEFINES} | ||
) | ||
endif() | ||
|
||
if(NOT _RULE_TESTONLY) | ||
install( | ||
TARGETS ${_RULE_NAME} | ||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${_RULE_SUBDIR} | ||
) | ||
endif() | ||
|
||
if(NOT "${_RULE_ALIAS}" STREQUAL "") | ||
add_library(${_RULE_ALIAS} ALIAS ${_RULE_NAME}) | ||
endif() | ||
endfunction() |
Oops, something went wrong.