Skip to content

Commit

Permalink
添加gn 构建运行
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaominghe2014 committed Jul 28, 2023
1 parent 3b09985 commit 8ffead7
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

.*
*.gn
out/*
xlib
cmake_build/*
build/*
obj/*
demo-encode
Expand Down
72 changes: 72 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 测试
executable("test_main") {
include_dirs = [
"include"
]
sources = [
"main.cpp",
"include/xlib.h",
"include/crypto/base64.h",
"include/crypto/md5.h",
"include/crypto/url.h",
"include/crypto/base85.h",
"include/crypto/sha1.h",
"include/net/XEpoll.h",
"include/net/http.h",
"include/net/XSocket.h",
"include/net/netconfig.h",
"include/memory/StackAlloc.h",
"include/memory/xallocator.h",
"include/algorithm/AStar.h",
"include/algorithm/dlx.h",
"include/algorithm/sort.h",
"include/algorithm/search.h",
"include/parser/SgfParser.h",
"include/parser/xml.h",
"include/parser/json.h",
"include/parser/YamlParser.h",
"include/math/matrix.h",
"include/math/util.h",
"include/math/math.h",
"include/macro/XDefine.h",
"include/macro/xplatform.h",
"include/macro/XBase.h",
"include/macro/XBinary.h",
"include/base/XRegex.h",
"include/base/XUtf8.h",
"include/base/XRegist.h",
"include/base/XLog.h",
"include/base/Serializer.h",
"include/base/XThread.h",
"include/base/XTime.h",
"include/base/XFileUtil.h",
"include/base/XString.h",
"include/base/XRandom.h",
"src/crypto/md5.cpp",
"src/crypto/base64.cpp",
"src/crypto/url.cpp",
"src/crypto/sha1.cpp",
"src/crypto/base85.cpp",
"src/net/XEpoll.cpp",
"src/net/XSocket.cpp",
"src/algorithm/sort.cpp",
"src/algorithm/search.cpp",
"src/algorithm/dlx.cpp",
"src/algorithm/AStar.cpp",
"src/parser/SgfParser.cpp",
"src/parser/YamlParser.cpp",
"src/parser/xml.cpp",
"src/parser/json.cpp",
"src/math/util.cpp",
"src/math/matrix.cpp",
"src/base/XRegex.cpp",
"src/base/XLog.cpp",
"src/base/XThread.cpp",
"src/base/XString.cpp",
"src/base/XFileUtil.cpp",
"src/base/XUtf8.cpp",
"src/base/XRandom.cpp",
"src/base/XTime.cpp",
]
}

5 changes: 5 additions & 0 deletions args.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Set build arguments here. See `gn help buildargs`.
host_os = "mac"
target_os = "mac"
current_os = "mac"
target_cpu = "x64"
24 changes: 24 additions & 0 deletions generate_sources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3

import os
import sys

# 获取所有源文件
sources = []
for root, dirs, files in os.walk('.'):
for file in files:
if any(file.endswith(ext) for ext in ['.h','.cpp']):
sources.append(os.path.join(root, file))

# 输出源文件列表
# sys.stdout.write(str(sources))

# 输出源文件列表
# sys.stdout.write(str(includes))

# 生成 GN 文件
with open('sources.gn', 'w') as f:
f.write('sources = [\n')
for source in sources:
f.write(' "{}",\n'.format(source))
f.write(']\n')
5 changes: 5 additions & 0 deletions gn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rm -rf out && \
mkdir out && \
gn gen ./out -v && \
ninja -C out test_main && \
./out/test_main
28 changes: 28 additions & 0 deletions gn/build/config/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("compiler_defaults") {
if (current_os == "linux") {
cflags = [
"-pthread",
"-std=c++14",
"-Wall",
"-Wextra",
"-g"
]
}

if(current_os == "mac") {
cflags = [
"-pthread",
"-std=c++14",
"-Wall",
"-Wextra",
"-g"
]
}
}

config("executable_ldconfig") {
}
28 changes: 28 additions & 0 deletions gn/build/config/BUILDCONFIG.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

is_linux = host_os == "linux" && current_os == "linux" && target_os == "linux"
is_mac = host_os == "mac" && current_os == "mac" && target_os == "mac"

# All binary targets will get this list of configs by default.
_shared_binary_target_configs = [ "//gn/build/config:compiler_defaults" ]

# Apply that default list to the binary target types.
set_defaults("executable") {
configs = _shared_binary_target_configs

# Executables get this additional configuration.
configs += [ "//gn/build/config:executable_ldconfig" ]
}
set_defaults("static_library") {
configs = _shared_binary_target_configs
}
set_defaults("shared_library") {
configs = _shared_binary_target_configs
}
set_defaults("source_set") {
configs = _shared_binary_target_configs
}

set_default_toolchain("//gn/build/config/toolchain:gcc")
84 changes: 84 additions & 0 deletions gn/build/config/toolchain/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

toolchain("gcc") {
tool("cc") {
depfile = "{{output}}.d"
command = "gcc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "CC {{output}}"
outputs =
[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
}

tool("cxx") {
depfile = "{{output}}.d"
command = "g++ -std=c++14 -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "CXX {{output}}"
outputs =
[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
}

tool("alink") {
command = "rm -f {{output}} && ar rcs {{output}} {{inputs}}"
description = "AR {{target_output_name}}{{output_extension}}"

outputs =
[ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ]
default_output_extension = ".a"
output_prefix = "lib"
}

tool("solink") {
soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
sofile = "{{output_dir}}/$soname"
rspfile = soname + ".rsp"
if (is_mac) {
os_specific_option = "-install_name @executable_path/$sofile"
rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
} else {
os_specific_option = "-Wl,-soname=$soname"
rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"
}

command = "g++ -shared {{ldflags}} -o $sofile $os_specific_option @$rspfile"

description = "SOLINK $soname"

# Use this for {{output_extension}} expansions unless a target manually
# overrides it (in which case {{output_extension}} will be what the target
# specifies).
default_output_extension = ".so"

# Use this for {{output_dir}} expansions unless a target manually overrides
# it (in which case {{output_dir}} will be what the target specifies).
default_output_dir = "{{root_out_dir}}"

outputs = [ sofile ]
link_output = sofile
depend_output = sofile
output_prefix = "lib"
}

tool("link") {
outfile = "{{target_output_name}}{{output_extension}}"
rspfile = "$outfile.rsp"
command = "g++ {{ldflags}} -o $outfile @$rspfile {{solibs}} {{libs}}"
description = "LINK $outfile"
default_output_dir = "{{root_out_dir}}"
rspfile_content = "{{inputs}}"
outputs = [ outfile ]
}

tool("stamp") {
command = "touch {{output}}"
description = "STAMP {{output}}"
}

tool("copy") {
command = "cp -af {{source}} {{output}}"
description = "COPY {{source}} {{output}}"
}
}
6 changes: 3 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
cmake -B ./build -DCMAKE_BUILD_TYPE=Release && \
cmake --build ./build --config Release && \
cd ./build && ctest -C Release --rerun-failed --output-on-failure --extra-verbose --debug
cmake -B ./cmake_build -DCMAKE_BUILD_TYPE=Release && \
cmake --build ./cmake_build --config Release && \
cd ./cmake_build && ctest -C Release --rerun-failed --output-on-failure --extra-verbose --debug

0 comments on commit 8ffead7

Please sign in to comment.