Skip to content

Instantly share code, notes, and snippets.

@gvanem
Last active May 29, 2022 18:34
Show Gist options
  • Select an option

  • Save gvanem/22c328b3388bc0adfb0e4bbb60e20de9 to your computer and use it in GitHub Desktop.

Select an option

Save gvanem/22c328b3388bc0adfb0e4bbb60e20de9 to your computer and use it in GitHub Desktop.
A GNU Makefile for the Vcpkg Package Manager; https://github.com/Microsoft/vcpkg.git
#
# GNU Makefile for the Vcpkg Package Manager (MSVC / clang-cl / MinGW).
#
# By <[email protected]> 2018 - 2022.
#
# Instructions:
# 1) Clone 'vcpkg' by:
# git clone https://github.com/Microsoft/vcpkg.git <SOME_DIR>
#
# 2) Clone 'vcpkg-tool' by:
# git clone https://github.com/Microsoft/vcpkg-tool.git <SOME_DIR>/vcpkg-tool
#
# 3) Put this 'Makefile.Windows' at:
# <SOME_DIR>/vcpkg-tool/src
#
# 4) Do 'set VCPKG_ROOT=<SOME_DIR>'.
#
# 5) And finally:
# cd <SOME_DIR>\vcpkg-tool\src
# make -f Makefile.Windows CC=cl all install
#
# and check the resulting 'vcpkg.exe'.
#
THIS_FILE = Makefile.Windows
DATE := $(shell date "+%d-%B-%Y")
DATE2 := $(shell date "+%Y %m %d")
#
# The following codes used in macro 'colour_msg' assumes you have
# MSys/Cygwin's echo with colour support.
#
BRIGHT_GREEN = \e[1;32m
BRIGHT_WHITE = \e[1;37m
colour_msg = @echo -e "$(1)\e[0m"
green_msg = $(call colour_msg,$(BRIGHT_GREEN)$(strip $(1)))
green_msg0 = $(call colour_msg,$(BRIGHT_GREEN)$(1))
white_msg = $(call colour_msg,$(BRIGHT_WHITE)$(strip $(1)))
#
# The 'VCPKG_BASE_VERSION' is the 'Y-M-D' numbers from '$(DATE2)'.
# The 'VCPKG_VERSION' comes from a 'git' command
#
VER_MAJOR = $(word 1, $(DATE2))
VER_MINOR = $(word 2, $(DATE2))
VER_PATCH = $(word 3, $(DATE2))
VCPKG_BASE_VERSION = $(VER_MAJOR)-$(VER_MINOR)-$(VER_PATCH)
VCPKG_VERSION = $(shell git -C ../../Git-latest/vcpkg-tool describe --dirty --always)
VCPKG_VERSION = master
#
# This library is required.
#
FMT_ROOT = ../../installed/$(CPU)-windows
#
# Options:
#
USE_STATIC_CRT ?= 1
USE_MP_COMPILE ?= 1
USE_STD_FILESYSTEM ?= 0
USE_ASTYLE_FORMATER ?= 1
#
# The root-directory for 'make install'.
#
MSVC_ROOT = $(realpath $(VSINSTALLDIR))
#
# For the 'CPP_preprocess' rule.
#
PYTHON ?= py -3
#
# The root-directory for:
# include/CppUnitTest*.h
# and
# lib/x86/Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib.
#
UNIT_TEST_ROOT = $(MSVC_ROOT)/VC/Auxiliary/VS/unittest
#
# Warn on undefined variables.
#
MAKEFLAGS += --warn-undefined-variables
#
# Make a list of .obj-files from .cpp:
#
cpp_to_obj = $(addprefix $(OBJ_DIR)/$(strip $(2)), \
$(notdir $(1:.cpp=.obj)))
#
# Similarily for preprocessed .i-files:
#
cpp_to_i = $(notdir $(1:.cpp=.i))
define Usage
Say "make -f $(THIS_FILE) [CC=xx] [all | clean | vclean | realclean | depend | install]"
Specify CC=gcc - use MinGW
Specify CC=cl - use MSVC
Specify CC=clang-cl - use clang-cl
endef
OBJ_DIR = objects
ifeq ($(CC),cl)
CC_HOST = MSVC
VCPKGLIB.LIB = vcpkglib.lib
MSVC_TYPE = 1
else ifeq ($(CC),clang-cl)
CC_HOST = clang-cl
VCPKGLIB.LIB = vcpkglib.lib
MSVC_TYPE = 1
else ifeq ($(CC),gcc)
CC_HOST = MinGW
VCPKGLIB.LIB = libvcpkglib.a
MSVC_TYPE = 0
else
$(error $(Usage))
endif
#
# Add as needed:
#
EXTRA_CFLAGS ?=
EXTRA_LDFLAGS ?=
#
# Common CFLAGS, LDFLAGS:
#
ifeq ($(MSVC_TYPE),1)
O = obj
CFLAGS = -nologo -Zi -Zo -Oy- \
-W4 -EHsc -Ot -GS- \
-std:c++17 -FI./config.h
ifeq ($(USE_STATIC_CRT),1)
CFLAGS += -MT
else
CFLAGS += -MD
endif
LDFLAGS = -nologo -subsystem:console -incremental:no -debug -verbose -map \
-nodefaultlib:oldnames.lib -nodefaultlib:uuid.lib -nodefaultlib:ole32.lib
else
O = o
CFLAGS = -m$(CC_BITS) -w4 -x c++ -std=c++17 -O2 -g --include ./config.h
LDFLAGS = -m$(CC_BITS) -s
RCFLAGS = -O COFF
endif
CFLAGS += -I. -I../include -I$(FMT_ROOT)/include
ifeq ($(CC),clang-cl)
CFLAGS += -ferror-limit=5 -fms-extensions
endif
ifeq ($(CPU),x86)
CC_BITS = 32
RCFLAGS += --target=pe-i386
else
CC_BITS = 64
RCFLAGS += --target=pe-x86-64
endif
#
# In case '-MP' is part of %CL, drop it.
#
export CL=
VPATH = vcpkg vcpkg/base vcpkg-fuzz
ifeq ($(MSVC_TYPE),1)
EX_LIBS = advapi32.lib bcrypt.lib version.lib shell32.lib
else
EX_LIBS = -ladvapi32 -lbcrypt -lversion
endif
default: all
test_ver:
$(call green_msg0, VER_MAJOR: '$(VER_MAJOR)')
$(call green_msg0, VER_MINOR: '$(VER_MINOR)')
$(call green_msg0, VER_PATCH: '$(VER_PATCH)')
$(call green_msg0, VCPKG_VERSION: '$(VCPKG_VERSION)')
$(call green_msg0, VCPKG_BASE_VERSION: '$(VCPKG_BASE_VERSION)')
#
# What to build:
#
TARGETS = vcpkg.exe \
vcpkg-fuzz.exe \
$(VCPKGLIB.LIB)
ifeq ($(MSVC_TYPE),1)
TARGETS += vcpkgtest.exe
endif
vcpkglib1_SRC = $(addprefix vcpkg/, \
archives.cpp \
binarycaching.cpp \
binaryparagraph.cpp \
build.cpp \
buildenvironment.cpp \
ci-baseline.cpp \
cmakevars.cpp \
commands.activate.cpp \
commands.add.cpp \
commands.add-version.cpp \
commands.autocomplete.cpp \
commands.buildexternal.cpp \
commands.cache.cpp \
commands.check-support.cpp \
commands.ci.cpp \
commands.ciclean.cpp \
commands.civerifyversions.cpp \
commands.contact.cpp \
commands.cpp \
commands.create.cpp \
commands.dependinfo.cpp \
commands.edit.cpp \
commands.env.cpp \
commands.fetch.cpp \
commands.find.cpp \
commands.format-manifest.cpp \
commands.generate-message-map.cpp \
commands.hash.cpp \
commands.info.cpp \
commands.init-registry.cpp \
commands.integrate.cpp \
commands.list.cpp \
commands.new.cpp \
commands.owns.cpp \
commands.portsdiff.cpp \
commands.porthistory.cpp \
commands.regenerate.cpp \
commands.search.cpp \
commands.setinstalled.cpp \
commands.use.cpp \
commands.update-baseline.cpp \
commands.upgrade.cpp \
commands.upload-metrics.cpp \
commands.version.cpp \
commands.xdownload.cpp \
commands.xvsinstances.cpp \
commands.zbootstrap-standalone.cpp \
commands.zce.cpp \
commands.zprintconfig.cpp \
configuration.cpp \
configure-environment.cpp \
dependencies.cpp \
export.cpp \
export.chocolatey.cpp \
export.ifw.cpp \
export.prefab.cpp \
globalstate.cpp \
help.cpp \
input.cpp \
install.cpp \
metrics.cpp \
packagespec.cpp \
paragraphs.cpp \
platform-expression.cpp \
portfileprovider.cpp \
postbuildlint.buildtype.cpp \
postbuildlint.cpp \
registries.cpp \
remove.cpp \
sourceparagraph.cpp \
spdx.cpp \
statusparagraph.cpp \
statusparagraphs.cpp \
tools.cpp \
triplet.cpp \
update.cpp \
userconfig.cpp \
vcpkgcmdarguments.cpp \
vcpkglib.cpp \
vcpkgpaths.cpp \
versiondeserializers.cpp \
versions.cpp \
visualstudio.cpp)
vcpkglib2_SRC = $(addprefix vcpkg/base/, \
checks.cpp \
chrono.cpp \
cofffilereader.cpp \
downloads.cpp \
git.cpp \
graphs.cpp \
hash.cpp \
json.cpp \
messages.cpp \
parse.cpp \
strings.cpp \
stringview.cpp \
system.cpp \
system.print.cpp \
system.proxy.cpp \
system.process.cpp \
uint128.cpp \
unicode.cpp \
uuid.cpp \
xmlserializer.cpp)
vcpkgtest_SRC = $(addprefix vcpkg-test/, \
arguments.cpp \
binarycaching.cpp \
cache.cpp \
catch.cpp \
chrono.cpp \
ci-baseline.cpp \
commands.cpp \
configmetadata.cpp \
configparser.cpp \
dependencies.cpp \
downloads.cpp \
expected.cpp \
export.cpp \
git.parse.cpp \
hash.cpp \
install.cpp \
integrate.cpp \
json.cpp \
manifests.cpp \
messages.cpp \
metrics.cpp \
mockcmakevarsprovider.cpp \
optional.cpp \
paragraph.cpp \
plan.cpp \
platform-expression.cpp \
spdx.cpp \
specifier.cpp \
statusparagraphs.cpp \
strings.cpp \
stringview.cpp \
system.cpp \
tools.cpp \
uint128.cpp \
update.cpp \
util.cpp \
util-tests.cpp \
versionplan.cpp)
vcpkglib1_OBJ = $(call cpp_to_obj, $(vcpkglib1_SRC))
vcpkglib2_OBJ = $(call cpp_to_obj, $(vcpkglib2_SRC))
vcpkglib_OBJ = $(vcpkglib1_OBJ) $(vcpkglib2_OBJ) \
$(OBJ_DIR)/files.$(O)
vcpkg_OBJ = $(call cpp_to_obj, vcpkg.cpp)
vcpkgtest_OBJ = $(call cpp_to_obj, $(vcpkgtest_SRC), test/)
vcpkglib_I = $(call cpp_to_i, $(vcpkglib1_SRC) $(vcpkglib2_SRC))
vcpkgtest_I = $(call cpp_to_i, $(vcpkgtest_SRC))
################################################################################################
ifeq ($(MSVC_TYPE),1)
$(vcpkgtest_OBJ) \
$(vcpkgtest_I): EXTRA_CFLAGS += -wd4505 -D_WINDLL -I$(UNIT_TEST_ROOT)/include
vcpkgtest.exe: EXTRA_LDFLAGS += -libpath:$(UNIT_TEST_ROOT)/lib ole32.lib
endif
GENERATED = config.h $(CC).args
all: dirs $(GENERATED) $(TARGETS) epilogue
epilogue:
$(call green_msg, Welcome to $(TARGETS).)
$(call green_msg, Do a $(BRIGHT_WHITE)make -f $(THIS_FILE) CC=$(CC) install$(BRIGHT_GREEN) at own risk.)
dirs:
- mkdir $(OBJ_DIR) $(OBJ_DIR)/test
config.h: $(THIS_FILE)
$(call Generate, $@,//)
$(file >> $@,$(config_h))
$(CC).args: $(THIS_FILE)
$(call white_msg, All CFLAGS are in $@.)
$(call create_resp_file, $@, -c $(CFLAGS))
install: all
cp --update $(filter %.lib, $(TARGETS)) $(MSVC_ROOT)/lib
cp --update $(filter %.exe, $(TARGETS)) $(MSVC_ROOT)/bin
cp --update $(filter %.pdb, $(TARGETS:.exe=.pdb)) $(MSVC_ROOT)/bin
vcpkg.exe: $(vcpkg_OBJ) $(VCPKGLIB.LIB) $(OBJ_DIR)/vcpkg.res
$(call link_EXE, $@, $^ $(EX_LIBS))
vcpkg-fuzz.exe: $(OBJ_DIR)/main.obj $(OBJ_DIR)/vcpkg-fuzz.res $(VCPKGLIB.LIB)
$(call link_EXE, $@, $^ advapi32.lib ole32.lib)
$(VCPKGLIB.LIB): $(vcpkglib_OBJ)
$(call create_lib, $@, $^)
$(OBJ_DIR)/vcpkg.rc: $(THIS_FILE)
$(call create_rc, $@, vcpkg.exe, VFT_APP)
$(OBJ_DIR)/vcpkg-fuzz.rc: $(THIS_FILE)
$(call create_rc, $@, vcpkg-fuzz.exe, VFT_APP)
vcpkgtest.exe: \
$(vcpkgtest_OBJ) \
$(OBJ_DIR)/test/files.$(O) \
$(OBJ_DIR)/test/registries.$(O) \
$(OBJ_DIR)/vcpkgtest.res \
$(VCPKGLIB.LIB)
$(call link_EXE, $@, $^ $(EX_LIBS))
$(OBJ_DIR)/vcpkgtest.rc: $(THIS_FILE)
$(call create_rc, $@, vcpkgtest.exe, VFT_APP)
#
# Special hacks for MSVC:
#
ifeq ($(CC),cl)
$(OBJ_DIR)/files.obj \
$(OBJ_DIR)/test/files.obj \
$(OBJ_DIR)/test/registries.obj: EXTRA_CFLAGS += -std:c++latest
endif
#
# Speed up the build using 'cl -MP ...'
#
ifeq ($(CC)$(USE_MP_COMPILE),cl1)
$(vcpkglib1_OBJ): $(vcpkglib1_SRC) | $(CC).args
$(call CPP_compile_MP, $(OBJ_DIR)\\, $(vcpkglib1_SRC))
$(vcpkglib2_OBJ): $(vcpkglib2_SRC) | $(CC).args
$(call CPP_compile_MP, $(OBJ_DIR)\\, $(vcpkglib2_SRC))
$(vcpkgtest_OBJ): $(vcpkgtest_SRC) | $(CC).args
$(call CPP_compile_MP, $(OBJ_DIR)/test\\, $(vcpkgtest_SRC))
endif
$(OBJ_DIR)/test/%.obj: vcpkg-test/%.cpp | $(CC).args
$(call CPP_compile, $@, $<)
$(OBJ_DIR)/%.obj: %.cpp | $(CC).args
$(call CPP_compile, $@, $<)
$(OBJ_DIR)/%.res: $(OBJ_DIR)/%.rc
$(call create_res, $@, $<)
%.i: %.cpp FORCE cpp_filter.py $(GENERATED)
$(call CPP_preprocess, $<, $@)
FORCE:
#
# '$(sort ..)' creates a unique list
#
link_junk = $(sort $(TARGETS:.exe=.{pdb,map}))
clean:
rm -fr $(OBJ_DIR)
rm -f $(GENERATED) $(link_junk) vc1*.pdb link.tmp link.args cpp_filter.py
realclean vclean: clean
rm -f $(TARGETS) .depend.Windows
#
# GNU-make macros.
#
define config_h
#ifndef vcpkg_config_h
#define vcpkg_config_h
/* Collect most '-Dxxx' value here to keep the cmd-line short.
* This file gets force-included by a '$$(CC) -FI./config.h ..'
*/
#define UNICODE
#define _UNICODE
#define NDEBUG
/* Enable inline code for '$(FMT_ROOT)/include/'
*/
#define FMT_HEADER_ONLY
/* The output of a 'git rev-parse HEAD' command:
*/
#define VCPKG_VERSION $(VCPKG_VERSION)
/* The output of a $$(shell date "+%Y %m %d") command:
*/
#define VCPKG_BASE_VERSION $(VCPKG_BASE_VERSION)
/* Do we need some extra functions?
*/
#define _WIN32_WINNT 0x0601
/* Turn off some warnings in the STL library.
*/
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#define VCPKG_USE_STD_FILESYSTEM $(USE_STD_FILESYSTEM)
/* Ignore the clang-cl / gcc warnings here to keep a short cmd-line.
*/
#if defined(__clang__) || defined(__GNUC__)
#define PRAGMA(x) _Pragma (#x)
#if defined(__clang__)
#define WARN_OFF(x) PRAGMA (clang diagnostic ignored x)
#else
#define WARN_OFF(x) PRAGMA (gnu diagnostic ignored x)
#endif
WARN_OFF ("-Wpragma-pack")
WARN_OFF ("-Wunknown-pragmas")
WARN_OFF ("-Wunused-function")
WARN_OFF ("-Wunused-variable")
WARN_OFF ("-Wmissing-field-initializers")
WARN_OFF ("-Wshadow-field-in-constructor")
WARN_OFF ("-Wnonportable-system-include-path")
WARN_OFF ("-Wzero-as-null-pointer-constant")
WARN_OFF ("-Wreorder-ctor")
WARN_OFF ("-Wsign-conversion")
WARN_OFF ("-Wnon-virtual-dtor")
WARN_OFF ("-Wmissing-noreturn")
WARN_OFF ("-Wreserved-id-macro")
WARN_OFF ("-Wc++17-extensions")
WARN_OFF ("-Wc++98-compat")
WARN_OFF ("-Wc++98-compat-pedantic")
WARN_OFF ("-Wglobal-constructors")
WARN_OFF ("-Wexit-time-destructors")
WARN_OFF ("-Wcast-calling-convention")
#endif
#if defined(__GNUC__)
#include <system_error>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <thread>
#else
/*
* This is supposed to speed things up using a pesky .pch file,
* but using 'cl -MP ..' is really more elegant.
*/
#include <pch.h>
#endif /* __GNUC__ */
#endif
endef
define Warning
$(1)
$(1) This file was generated by '$(THIS_FILE)' at $(DATE).
$(1) DO NOT EDIT. YOUR CHANGED WILL BE LOST.
$(1)
endef
define Generate
$(call green_msg, Generating $(1))
$(file > $(1),$(call Warning,$(2)))
endef
#
# Compile a .cpp-file:
# arg1 = $(1): the .obj-file
# arg2 = $(2): the .cpp-file + any extra CFLAGS.
#
ifeq ($(MSVC_TYPE),1)
define CPP_compile
$(CC) @$(CC).args -Fo./$(strip $(1) $(EXTRA_CFLAGS) $(2))
@echo
endef
#
# Compile several .cpp-files, MSVC only:
# arg1 = $(1): the .obj-file directory
# arg2 = $(2): the .cpp-files + any extra CFLAGS.
#
define CPP_compile_MP
$(call green_msg, Compiling $(words $(2)) .cpp-files...)
$(CC) -MP @$(CC).args -Fo$(strip $(1) $(2))
@echo
endef
else
define CPP_compile
$(CC) @$(CC).args -o $(strip $(1) $(EXTRA_CFLAGS) $(2))
@echo
endef
endif
#
# Link a .EXE-file:
# arg1 = $(1): the .EXE-file
# arg2 = $(2): the .obj-files + needed libraries
#
define link_EXE
$(call green_msg, Linking $(1))
$(call create_resp_file, link.args, $(LDFLAGS) $(EXTRA_LDFLAGS) $(2))
$(call link_EXE_$(MSVC_TYPE), $(1))
@echo
endef
define link_EXE_0
$(CC) -o $(1) @link.args
endef
define link_EXE_1
link -out:$(strip $(1)) @link.args > link.tmp
@cat link.tmp >> $(1:.exe=.map)
@rm -f link.tmp
endef
#
# Create a response file:
# arg1 = $(1): the file.
# arg2 = $(2): the input lines.
#
define create_resp_file
$(file > $(1))
$(foreach w, $(2), $(file >> $(1),$(w)))
endef
#
# Create a static library
# arg1 = $(1): the output .lib file.
# arg2 = $(2): the input .obj files.
#
define create_lib
$(call green_msg, Creating library $(1))
$(call create_lib_$(MSVC_TYPE), $(1), $(2))
@echo
endef
create_lib_0 = ar rs $(strip $(1) $(2))
create_lib_1 = lib -nologo -out:$(strip $(1) $(2))
#
# Create a .res-file:
# arg1 = $(1): the output .res-file.
# arg2 = $(2): the input .rc-file.
#
define create_res
$(call green_msg, Generating resource $(1))
$(call create_res_$(MSVC_TYPE), $(1), $(2))
@echo
endef
create_res_0 = windres $(RCFLAGS) -o $(strip $(1) $(2))
create_res_1 = rc -nologo -fo $(strip $(1) $(2))
#
# Create a .rc-file:
# arg1 = $(1): the output .rc-file.
# arg2 = $(2): the RC_FILE_NAME
# arg3 = $(3): the RC_FILE_TYPE
#
define create_rc
$(call Generate, $(1),//)
$(file >> $@, #define RC_FILE_NAME "$(strip $(2))")
$(file >> $@, #define RC_FILE_TYPE $(3))
$(file >> $@,$(common_rc))
endef
#
# Common contents for all generated *.rc files
#
define common_rc
/*
* THIS FILE IS GENERATED. DO NOT EDIT!!
*/
#include <winver.h>
#ifndef RC_FILE_NAME
#error "Define 'RC_FILE_NAME' before including me."
#endif
#ifndef RC_FILE_TYPE
#error "Define 'RC_FILE_TYPE' to either 'VFT_APP' or 'VFT_DLL' before including me."
#endif
/* Where should these version-triplets really come from? Not in any .h-file.
* So give all PE-files the same version.
*/
#define RC_VERSION $(VER_MAJOR),$(VER_MINOR),$(VER_PATCH),0
#define RC_VER_STR "$(VER_MAJOR).$(VER_MINOR).$(VER_PATCH)"
LANGUAGE 0x09,0x01
VS_VERSION_INFO VERSIONINFO
FILEVERSION RC_VERSION
PRODUCTVERSION RC_VERSION
FILEFLAGSMASK 0x3fL
FILEFLAGS 0
FILEOS VOS_NT_WINDOWS32
FILETYPE RC_FILE_TYPE
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "Microsoft."
VALUE "FileDescription", "Part of Vcpkg; https://github.com/Microsoft/vcpkg.git"
VALUE "FileVersion", RC_VER_STR
VALUE "InternalName", RC_FILE_NAME " ($(CC_HOST), $(CC_BITS)-bits)"
VALUE "OriginalFilename", RC_FILE_NAME
VALUE "ProductName", "A C/C++ Library/Package Manager."
VALUE "ProductVersion", RC_VER_STR
VALUE "LegalCopyright", ""
VALUE "Comments", "Built on $(DATE)"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
endef
#
# C++ preprocess macro.
#
ifeq ($(USE_ASTYLE_FORMATER),1)
define CPP_preprocess
$(file > $(2),/* The preprocessed and 'astyle' formatted output of '$(strip $(1))':)
$(file >> $(2), * $(CC) -E)
@$(foreach f, $(CFLAGS), $(file >> $(2), * $(f)))
$(file >> $(2), *---------------------------------------------------------)
$(file >> $(2), */)
$(CC) -E @$(CC).args $(1) | $(PYTHON) cpp_filter.py | astyle >> $(2)
@echo
endef
else
define CPP_preprocess
$(file > $(2),/* The raw preprocessed output of '$(strip $(1))':)
$(file >> $(2), * $(CC) -E)
@$(foreach f, $(CFLAGS), $(file >> $(2), * $(f)))
$(file >> $(2), *---------------------------------------------------------)
$(file >> $(2), */)
$(CC) -E @$(CC).args $(1) | $(PYTHON) cpp_filter.py >> $(2)
@echo
endef
endif
define CPP_FILTER_PY
import sys, os, ntpath
def _win32_abspath (path):
path = ntpath.abspath (path)
return path.replace ('\\', '/')
cwd = _win32_abspath (os.getcwd()) + '/'
last_line = '??'
last_fname = '??'
empty_lines = 0
while True:
line = sys.stdin.readline()
if not line:
break
if line.startswith('\n') or line.startswith('\r'):
empty_lines += 1
continue
if line.lstrip().startswith("#line"):
line = line.replace ("\\\\", "/")
fname = None
quote = line.find ('\"')
if line.startswith ("#line ") and quote > 0:
fname = _win32_abspath (line[quote:])
last_fname = fname
if line.strip() != '' and last_line != '':
if fname is None or fname != last_fname:
print (line, end="")
if line.strip() == '}' or line.strip() == '};': # Print a newline after a functions or structs
print ("")
last_line = line
if empty_lines > 0:
print ("Removed %d empty lines." % empty_lines, file=sys.stderr)
endef
cpp_filter.py: $(THIS_FILE)
$(call green_msg, Generating $@...)
$(file > $@,#!/usr/bin/env python)
$(file >> $@,#)
$(file >> $@,# DO NOT EDIT! This file was generated automatically)
$(file >> $@,# from $(realpath $<). Edit that file instead.)
$(file >> $@,#)
$(file >> $@,from __future__ import print_function)
$(file >> $@,if 1:)
$(file >> $@,$(CPP_FILTER_PY))
#
# Generate dependencies using 'g++ -MM'. Handy for MSVC/clang-cl too (that is, if you have g++).
#
DEP_REPLACE_1 = sed -e 's|\(.*\)\.o: |\n$$(OBJ_DIR)\/\1.obj: |'
DEP_REPLACE_2 = sed -e 's|\(.*\)\.o: |\n$$(OBJ_DIR)\/test/\1.obj: |'
define make_depend
$(call Generate, .depend.Windows, #)
g++ -std=c++11 -MM $(filter -D% -I%, $(CFLAGS)) $(1) | $(2) >> .depend.Windows
endef
depend: $(GENERATED)
$(call make_depend, $(vcpkglib1_SRC) $(vcpkglib2_SRC) vcpkg.cpp, $(DEP_REPLACE_1))
$(call make_depend, $(vcpkgtest_SRC), $(DEP_REPLACE_2))
-include .depend.Windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment