cc_project_template_gn

A gn, ninja, and googletest-based C++ project template
git clone https://git.bracken.jp/cc_project_template_gn.git
Log | Files | Refs | Submodules | README | LICENSE

commit 7ea0769a343dbbfd63d1c9e5bd155bd19ef747e0
parent ffd0fb7ed3e9202cf87094653878b5ae4e65a74e
Author: Chris Bracken <chris@bracken.jp>
Date:   Fri,  5 Oct 2018 09:47:47 -0700

Add minimal gn clang toolchain

Adds a minimal clang toolchain definition that relies on clang binaries
installed on the host.

Diffstat:
Abuild/BUILD.gn | 12++++++++++++
Abuild/BUILDCONFIG.gn | 37+++++++++++++++++++++++++++++++++++++
Abuild/toolchain/BUILD.gn | 147+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 196 insertions(+), 0 deletions(-)

diff --git a/build/BUILD.gn b/build/BUILD.gn @@ -0,0 +1,12 @@ +config("compiler_defaults") { + cflags = [ + "-std=c++17", + "-Weverything", + "-Wno-c++98-compat", + ] +} + +config("executable_ldconfig") { + ldflags = [ + ] +} diff --git a/build/BUILDCONFIG.gn b/build/BUILDCONFIG.gn @@ -0,0 +1,37 @@ +if (target_os == "") { + target_os = host_os +} +if (target_cpu == "") { + target_cpu = host_cpu +} +if (current_cpu == "") { + current_cpu = target_cpu +} +if (current_os == "") { + current_os = target_os +} + +# Options +use_strip = false + +# All binary targets will get this list of configs by default. +_shared_binary_target_configs = [ "//build: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 += [ "//build: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("//build/toolchain:clang") diff --git a/build/toolchain/BUILD.gn b/build/toolchain/BUILD.gn @@ -0,0 +1,147 @@ +# Each toolchain must define "stamp" and "copy" tools, +# but they are always the same in every toolchain. +stamp_command = "touch {{output}}" +stamp_description = "STAMP {{output}}" + +# We use link instead of copy; the way "copy" tool is being used is +# compatible with links since Ninja is tracking changes to the source. +copy_command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})" +copy_description = "COPY {{source}} {{output}}" + +toolchain("clang") { + cc = "clang" + cxx = "clang++" + ld = cxx + ar = "llvm-ar" + + tool("cc") { + depfile = "{{output}}.d" + command = "$cc -MD -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 = "$cxx -MD -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("asm") { + depfile = "{{output}}.d" + command = "$cc -MD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}" + depsformat = "gcc" + description = "ASM {{output}}" + outputs = [ + "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", + ] + } + + tool("objc") { + depfile = "{{output}}.d" + command = "$cc -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} {{cflags_objc}} -c {{source}} -o {{output}}" + depsformat = "gcc" + description = "OBJC {{output}}" + outputs = [ + "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", + ] + } + + tool("objcxx") { + depfile = "{{output}}.d" + command = "$cxx -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} {{cflags_objcc}} -c {{source}} -o {{output}}" + depsformat = "gcc" + description = "OBJCXX {{output}}" + outputs = [ + "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", + ] + } + + tool("alink") { + rspfile = "{{output}}.rsp" + command = "rm -f {{output}} && $ar {{arflags}} rcsD {{output}} @\"$rspfile\"" + description = "AR {{output}}" + rspfile_content = "{{inputs}}" + outputs = [ + "{{output_dir}}/{{target_output_name}}{{output_extension}}", + ] + default_output_dir = "{{target_out_dir}}" + default_output_extension = ".a" + output_prefix = "lib" + } + + tool("solink") { + outname = "{{target_output_name}}{{output_extension}}" + outfile = "{{output_dir}}/$outname" + rspfile = "$outfile.rsp" + unstripped_outfile = outfile + if (use_strip) { + unstripped_outfile = "{{output_dir}}/lib.unstripped/{{target_output_name}}{{output_extension}}" + } + if (target_os == "mac") { + command = "$ld -shared {{ldflags}} -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" -o \"$unstripped_outfile\" -Wl,-filelist,\"$rspfile\" {{libs}} {{solibs}}" + rspfile_content = "{{inputs_newline}}" + default_output_extension = ".dylib" + } else { + command = "$ld -shared {{ldflags}} -o \"$unstripped_outfile\" -Wl,--Map=\"$unstripped_outfile.map\" -Wl,-soname=\"$outname\" @\"$rspfile\"" + rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}" + default_output_extension = ".so" + } + if (use_strip) { + command += " && strip --strip-all \"$unstripped_outfile\" \"$outfile\"" + } + description = "SOLINK $outfile" + default_output_dir = "{{root_out_dir}}" + output_prefix = "lib" + outputs = [ + outfile, + ] + if (outfile != unstripped_outfile) { + outputs += [ unstripped_outfile ] + } + } + + tool("link") { + outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" + rspfile = "$outfile.rsp" + unstripped_outfile = outfile + if (use_strip) { + unstripped_outfile = "{{root_out_dir}}/exe.unstripped/{{target_output_name}}{{output_extension}}" + } + if (target_os == "mac") { + command = "$ld {{ldflags}} -o \"$unstripped_outfile\" -Wl,-filelist,\"$rspfile\" {{solibs}} {{libs}}" + rspfile_content = "{{inputs_newline}}" + } else { + command = "$ld {{ldflags}} -o \"$unstripped_outfile\" -Wl,--Map=\"$unstripped_outfile.map\" -Wl,--start-group @\"$rspfile\" {{solibs}} -Wl,--end-group {{libs}}" + rspfile_content = "{{inputs}}" + } + if (use_strip) { + command += " && strip --strip-sections \"$unstripped_outfile\" \"$outfile\"" + } + description = "LINK $outfile" + default_output_dir = "{{root_out_dir}}" + outputs = [ + outfile, + ] + if (outfile != unstripped_outfile) { + outputs += [ unstripped_outfile ] + } + } + + tool("stamp") { + command = stamp_command + description = stamp_description + } + + tool("copy") { + command = copy_command + description = copy_description + } +}