o
    bi                    @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dlm
Z
 d dlZddddddd	d
dddddddZdZdag ag ag adadd Zdd ZdZdZdZdZdZdZde d e d  e d! e d" e d# Zd$Zd%d& Zd'Zd(Zd)Z d*Z!d+d,d,d,d+d+d-Z"d.d/ Z#d0d1 Z$d2d3 Z%d4d5 Z&d6d7 Z'd8d9 Z(d:d; Z)d<d= Z*da+d>d? Z,dMdAdBZ-dCdD Z.i Z/i Z0G dEdF dFe1Z2dGdH Z3dIdJ Z4dKdL Z5dS )N    )print_functionN)GetEnvironFallback)GypError lib.az $(obj).$(TOOLSET)/$(TARGET)/geniz
$(obj)/gen$(builddir)%(INPUT_ROOT)s%(INPUT_DIRNAME)sz$(abspath $<)z$(suffix $<)z$(notdir $<)$(BUILDTYPE))EXECUTABLE_PREFIXEXECUTABLE_SUFFIXSTATIC_LIB_PREFIXSHARED_LIB_PREFIXSTATIC_LIB_SUFFIXINTERMEDIATE_DIRSHARED_INTERMEDIATE_DIRPRODUCT_DIRRULE_INPUT_ROOTRULE_INPUT_DIRNAMERULE_INPUT_PATHRULE_INPUT_EXTRULE_INPUT_NAMECONFIGURATION_NAMETFc                 C   s   t j|}|dkrK| dd | dd | dtd  | dtd  dd	lm  m} t|d
g a	t|dg a
t|dg atddd d	S |}|dkrSd}| d| |dkrd| dd n| dd | dd | dd d	S )zDCalculate additional variables for use in the build (called by gyp).macOSSHARED_LIB_SUFFIXz.dylibSHARED_LIB_DIRr   LIB_DIRr   N+generator_additional_non_configuration_keys"generator_additional_path_sections!generator_extra_sources_for_rulesobjcobjcxx).m.mmandroidlinuxaixr   .soz$(builddir)/lib.$(TOOLSET)z$(obj).$(TOOLSET))gypcommon	GetFlavor
setdefaultgenerator_default_variablesgyp.generator.xcode	generatorxcodegetattrr   r    r!   COMPILABLE_EXTENSIONSupdate)default_variablesparamsflavorxcode_generatoroperating_system r:   4/usr/lib/python3/dist-packages/gyp/generator/make.pyCalculateVariablesD   s<   r<   c                 C   sl   |  di }| dd}|rda| d jp| d j}| dd}tjtj||d}| d j|d	adS )
zOCalculate the generator specific info that gets fed to input (called by
  gyp).generator_flagsandroid_ndk_versionNToptions
output_diroutgypfiles)toplevelqualified_out_dir)	get#generator_wants_sorted_dependenciesgenerator_outputtoplevel_dirospathnormpathjoingenerator_filelist_paths)r6   r=   r>   r@   builddir_namerD   r:   r:   r;   CalculateGeneratorInputInfoi   s   

rO   ?a  quiet_cmd_alink = AR($(TOOLSET)) $@
cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crs $@ $(filter %.o,$^)

quiet_cmd_alink_thin = AR($(TOOLSET)) $@
cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^)

# Due to circular dependencies between libraries :(, we wrap the
# special "figure out circular dependencies" flags around the entire
# input list during linking.
quiet_cmd_link = LINK($(TOOLSET)) $@
cmd_link = $(LINK.$(TOOLSET)) -o $@ $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,--start-group $(LD_INPUTS) $(LIBS) -Wl,--end-group

# We support two kinds of shared objects (.so):
# 1) shared_library, which is just bundling together many dependent libraries
# into a link line.
# 2) loadable_module, which is generating a module intended for dlopen().
#
# They differ only slightly:
# In the former case, we want to package all dependent code into the .so.
# In the latter case, we want to package just the API exposed by the
# outermost module.
# This means shared_library uses --whole-archive, while loadable_module doesn't.
# (Note that --whole-archive is incompatible with the --start-group used in
# normal linking.)

# Other shared-object link notes:
# - Set SONAME to the library filename so our binaries don't reference
# the local, absolute paths used on the link command-line.
quiet_cmd_solink = SOLINK($(TOOLSET)) $@
cmd_solink = $(LINK.$(TOOLSET)) -o $@ -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -Wl,--whole-archive $(LD_INPUTS) -Wl,--no-whole-archive $(LIBS)

quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module = $(LINK.$(TOOLSET)) -o $@ -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -Wl,--start-group $(filter-out FORCE_DO_CMD, $^) -Wl,--end-group $(LIBS)
ah  quiet_cmd_alink = LIBTOOL-STATIC $@
cmd_alink = rm -f $@ && ./gyp-mac-tool filter-libtool libtool $(GYP_LIBTOOLFLAGS) -static -o $@ $(filter %.o,$^)

quiet_cmd_link = LINK($(TOOLSET)) $@
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)

quiet_cmd_solink = SOLINK($(TOOLSET)) $@
cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)

quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module = $(LINK.$(TOOLSET)) -bundle $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)
a  quiet_cmd_alink = AR($(TOOLSET)) $@
cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crs $@ $(filter %.o,$^)

quiet_cmd_alink_thin = AR($(TOOLSET)) $@
cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^)

# Due to circular dependencies between libraries :(, we wrap the
# special "figure out circular dependencies" flags around the entire
# input list during linking.
quiet_cmd_link = LINK($(TOOLSET)) $@
quiet_cmd_link_host = LINK($(TOOLSET)) $@
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ -Wl,--start-group $(LD_INPUTS) -Wl,--end-group $(LIBS)
cmd_link_host = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(LD_INPUTS) $(LIBS)

# Other shared-object link notes:
# - Set SONAME to the library filename so our binaries don't reference
# the local, absolute paths used on the link command-line.
quiet_cmd_solink = SOLINK($(TOOLSET)) $@
cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ -Wl,--whole-archive $(LD_INPUTS) -Wl,--no-whole-archive $(LIBS)

quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ -Wl,--start-group $(filter-out FORCE_DO_CMD, $^) -Wl,--end-group $(LIBS)
quiet_cmd_solink_module_host = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module_host = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)
a  quiet_cmd_alink = AR($(TOOLSET)) $@
cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) -X32_64 crs $@ $(filter %.o,$^)

quiet_cmd_alink_thin = AR($(TOOLSET)) $@
cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) -X32_64 crs $@ $(filter %.o,$^)

quiet_cmd_link = LINK($(TOOLSET)) $@
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(LD_INPUTS) $(LIBS)

quiet_cmd_solink = SOLINK($(TOOLSET)) $@
cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(LD_INPUTS) $(LIBS)

quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)
a  quiet_cmd_alink = AR($(TOOLSET)) $@
cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crs $@ $(filter %.o,$^)

quiet_cmd_alink_thin = AR($(TOOLSET)) $@
cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^)

quiet_cmd_link = LINK($(TOOLSET)) $@
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(LD_INPUTS) $(LIBS)

quiet_cmd_solink = SOLINK($(TOOLSET)) $@
cmd_solink = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(LD_INPUTS) $(LIBS) -Wl,DLL

quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -Wl,DLL
a.	  # We borrow heavily from the kernel build setup, though we are simpler since
# we don't have Kconfig tweaking settings on us.

# The implicit make rules have it looking for RCS files, among other things.
# We instead explicitly write all the rules we care about.
# It's even quicker (saves ~200ms) to pass -r on the command line.
MAKEFLAGS=-r

# The source directory tree.
srcdir := %(srcdir)s
abs_srcdir := $(abspath $(srcdir))

# The name of the builddir.
builddir_name ?= %(builddir)s

# The V=1 flag on command line makes us verbosely print command lines.
ifdef V
  quiet=
else
  quiet=quiet_
endif

# Specify BUILDTYPE=Release on the command line for a release build.
BUILDTYPE ?= %(default_configuration)s

# Directory all our build output goes into.
# Note that this must be two directories beneath src/ for unit tests to pass,
# as they reach into the src/ directory for data with relative paths.
builddir ?= $(builddir_name)/$(BUILDTYPE)
abs_builddir := $(abspath $(builddir))
depsdir := $(builddir)/.deps

# Object output directory.
obj := $(builddir)/obj
abs_obj := $(abspath $(obj))

# We build up a list of every single one of the targets so we can slurp in the
# generated dependency rule Makefiles in one pass.
all_deps :=

%(make_global_settings)s

CC.target ?= %(CC.target)s
CFLAGS.target ?= $(CPPFLAGS) $(CFLAGS)
CXX.target ?= %(CXX.target)s
CXXFLAGS.target ?= $(CPPFLAGS) $(CXXFLAGS)
LINK.target ?= %(LINK.target)s
LDFLAGS.target ?= $(LDFLAGS)
AR.target ?= $(AR)

# C++ apps need to be linked with g++.
LINK ?= $(CXX.target)

# TODO(evan): move all cross-compilation logic to gyp-time so we don't need
# to replicate this environment fallback in make as well.
CC.host ?= %(CC.host)s
CFLAGS.host ?= $(CPPFLAGS_host) $(CFLAGS_host)
CXX.host ?= %(CXX.host)s
CXXFLAGS.host ?= $(CPPFLAGS_host) $(CXXFLAGS_host)
LINK.host ?= %(LINK.host)s
LDFLAGS.host ?= $(LDFLAGS_host)
AR.host ?= %(AR.host)s

# Define a dir function that can handle spaces.
# http://www.gnu.org/software/make/manual/make.html#Syntax-of-Functions
# "leading spaces cannot appear in the text of the first argument as written.
# These characters can be put into the argument value by variable substitution."
empty :=
space := $(empty) $(empty)

# http://stackoverflow.com/questions/1189781/using-make-dir-or-notdir-on-a-path-with-spaces
replace_spaces = $(subst $(space),z ,$1)
unreplace_spaces = $(subst a  ,$(space),$1)
dirx = $(call unreplace_spaces,$(dir $(call replace_spaces,$1)))

# Flags to make gcc output dependency info.  Note that you need to be
# careful here to use the flags that ccache and distcc can understand.
# We write to a dep file on the side first and then rename at the end
# so we can't end up with a broken dep file.
depfile = $(depsdir)/$(call replace_spaces,$@).d
DEPFLAGS = %(makedep_args)s -MF $(depfile).raw

# We have to fixup the deps output in a few ways.
# (1) the file output should mention the proper .o file.
# ccache or distcc lose the path to the target, so we convert a rule of
# the form:
#   foobar.o: DEP1 DEP2
# into
#   path/to/foobar.o: DEP1 DEP2
# (2) we want missing files not to cause us to fail to build.
# We want to rewrite
#   foobar.o: DEP1 DEP2 \
#               DEP3
# to
#   DEP1:
#   DEP2:
#   DEP3:
# so if the files are missing, they're just considered phony rules.
# We have to do some pretty insane escaping to get those backslashes
# and dollar signs past make, the shell, and sed at the same time.
# Doesn't work with spaces, but that's fine: .d files have spaces in
# their names replaced with other characters.
define fixup_dep
# The depfile may not exist if the input file didn't have any #includes.
touch $(depfile).raw
# Fixup path as in (1).
sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile)
# Add extra rules as in (2).
# We remove slashes and replace spaces with new lines;
# remove blank lines;
# delete the first line and append a colon to the remaining lines.
sed -e 's|\\||' -e 'y| |\n|' $(depfile).raw |\
  grep -v '^$$'                             |\
  sed -e 1d -e 's|$$|:|'                     \
    >> $(depfile)
rm $(depfile).raw
endef

# Command definitions:
# - cmd_foo is the actual command to run;
# - quiet_cmd_foo is the brief-output summary of the command.

quiet_cmd_cc = CC($(TOOLSET)) $@
cmd_cc = $(CC.$(TOOLSET)) -o $@ $< $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c

quiet_cmd_cxx = CXX($(TOOLSET)) $@
cmd_cxx = $(CXX.$(TOOLSET)) -o $@ $< $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c
%(extra_commands)s
quiet_cmd_touch = TOUCH $@
cmd_touch = touch $@

quiet_cmd_copy = COPY $@
# send stderr to /dev/null to ignore messages when linking directories.
cmd_copy = ln -f "$<" "$@" 2>/dev/null || (rm -rf "$@" && cp %(copy_archive_args)s "$<" "$@")

%(link_commands)s

# Define an escape_quotes function to escape single quotes.
# This allows us to handle quotes properly as long as we always use
# use single quotes and escape_quotes.
escape_quotes = $(subst ','\'',$(1))
# This comment is here just to include a ' to unconfuse syntax highlighting.
# Define an escape_vars function to escape '$' variable syntax.
# This allows us to read/write command lines with shell variables (e.g.
# $LD_LIBRARY_PATH), without triggering make substitution.
escape_vars = $(subst $$,$$$$,$(1))
# Helper that expands to a shell command to echo a string exactly as it is in
# make. This uses printf instead of echo because printf's behaviour with respect
# to escape sequences is more portable than echo's across different shells
# (e.g., dash, bash).
exact_echo = printf '%%s\n' '$(call escape_quotes,$(1))'

# Helper to compare the command we're about to run against the command
# we logged the last time we ran the command.  Produces an empty
# string (false) when the commands match.
# Tricky point: Make has no string-equality test function.
# The kernel uses the following, but it seems like it would have false
# positives, where one string reordered its arguments.
#   arg_check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
#                       $(filter-out $(cmd_$@), $(cmd_$(1))))
# We instead substitute each for the empty string into the other, and
# say they're equal if both substitutions produce the empty string.
# .d files contain aa   instead of spaces, take that into account.
command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))),\
                       $(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1))))

# Helper that is non-empty when a prerequisite changes.
# Normally make does this implicitly, but we force rules to always run
# so we can check their command lines.
#   $? -- new prerequisites
#   $| -- order-only dependencies
prereq_changed = $(filter-out FORCE_DO_CMD,$(filter-out $|,$?))

# Helper that executes all postbuilds until one fails.
define do_postbuilds
  @E=0;\
  for p in $(POSTBUILDS); do\
    eval $$p;\
    E=$$?;\
    if [ $$E -ne 0 ]; then\
      break;\
    fi;\
  done;\
  if [ $$E -ne 0 ]; then\
    rm -rf "$@";\
    exit $$E;\
  fi
endef

# do_cmd: run a command via the above cmd_foo names, if necessary.
# Should always run for a given target to handle command-line changes.
# Second argument, if non-zero, makes it do asm/C/C++ dependency munging.
# Third argument, if non-zero, makes it do POSTBUILDS processing.
# Note: We intentionally do NOT call dirx for depfile, since it contains z* for
# spaces already and dirx strips the a   characters.
define do_cmd
$(if $(or $(command_changed),$(prereq_changed)),
  @$(call exact_echo,  $($(quiet)cmd_$(1)))
  @mkdir -p "$(call dirx,$@)" "$(dir $(depfile))"
  $(if $(findstring flock,$(word %(flock_index)d,$(cmd_$1))),
    @$(cmd_$(1))
    @echo "  $(quiet_cmd_$(1)): Finished",
    @$(cmd_$(1))
  )
  @$(call exact_echo,$(call escape_vars,cmd_$(call replace_spaces,$@) := $(cmd_$(1)))) > $(depfile)
  @$(if $(2),$(fixup_dep))
  $(if $(and $(3), $(POSTBUILDS)),
    $(call do_postbuilds)
  )
)
endef

# Declare the "%(default_target)s" target first so it is the default,
# even though we don't have the deps yet.
.PHONY: %(default_target)s
%(default_target)s:

# make looks for ways to re-generate included makefiles, but in our case, we
# don't have a direct way. Explicitly telling make that it has nothing to do
# for them makes it go faster.
%%.d: ;

# Use FORCE_DO_CMD to force a target to run.  Should be coupled with
# do_cmd.
.PHONY: FORCE_DO_CMD
FORCE_DO_CMD:

a  
quiet_cmd_objc = CXX($(TOOLSET)) $@
cmd_objc = $(CC.$(TOOLSET)) $(GYP_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<

quiet_cmd_objcxx = CXX($(TOOLSET)) $@
cmd_objcxx = $(CXX.$(TOOLSET)) $(GYP_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<

# Commands for precompiled header files.
quiet_cmd_pch_c = CXX($(TOOLSET)) $@
cmd_pch_c = $(CC.$(TOOLSET)) $(GYP_PCH_CFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_pch_cc = CXX($(TOOLSET)) $@
cmd_pch_cc = $(CC.$(TOOLSET)) $(GYP_PCH_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_pch_m = CXX($(TOOLSET)) $@
cmd_pch_m = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
quiet_cmd_pch_mm = CXX($(TOOLSET)) $@
cmd_pch_mm = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<

# gyp-mac-tool is written next to the root Makefile by gyp.
# Use $(4) for the command, since $(2) and $(3) are used as flag by do_cmd
# already.
quiet_cmd_mac_tool = MACTOOL $(4) $<
cmd_mac_tool = ./gyp-mac-tool $(4) $< "$@"

quiet_cmd_mac_package_framework = PACKAGE FRAMEWORK $@
cmd_mac_package_framework = ./gyp-mac-tool package-framework "$@" $(4)

quiet_cmd_infoplist = INFOPLIST $@
cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@"
c                 C   s   t t tjd}| d |D ]}| d|  | dt|   q| d |D ]}| d|  | dt|   q*| d |D ]}| d|  | dt|   qD| d d S )	N)key1# Suffix rules, putting all outputs into $(obj).
z4$(obj).$(TOOLSET)/%%.o: $(srcdir)/%%%s FORCE_DO_CMD
z	@$(call do_cmd,%s,1)
z,
# Try building from generated source, too.
z<$(obj).$(TOOLSET)/%%.o: $(obj).$(TOOLSET)/%%%s FORCE_DO_CMD

z1$(obj).$(TOOLSET)/%%.o: $(obj)/%%%s FORCE_DO_CMD
)sortedr3   keysstrlowerwrite)writer
extensionsextr:   r:   r;   WriteRootHeaderSuffixRules  s    


r\   rR   z+# Try building from generated source, too.
a  # "all" is a concatenation of the "all" targets from all the included
# sub-makefiles. This is just here to clarify.
all:

# Add in dependency-tracking rules.  $(all_deps) is the list of every single
# target in our tree. Only consider the ones with .d (dependency) info:
d_files := $(wildcard $(foreach f,$(all_deps),$(depsdir)/$(f).d))
ifneq ($(d_files),)
  include $(d_files)
endif
z/# This file is generated by gyp; do not edit.

cccxx).c.cc.cpp.cxx.s.Sc                    s&    fddt D D ]}|r dS q	dS )z:Return true if the file is compilable (should be in OBJS).c                 3   s    | ]}  |V  qd S Nendswith).0efilenamer:   r;   	<genexpr>J  s    zCompilable.<locals>.<genexpr>TF)r3   )rk   resr:   rj   r;   
CompilableH  s
   rn   c                 C   s
   |  dS )zAReturn true if the file is linkable (should be on the link line)..orf   rj   r:   r:   r;   LinkableP  s   
rp   c                 C   s   t j| d d S )z1Translate a compilable filename to its .o target.r   ro   rI   rJ   splitextrj   r:   r:   r;   TargetU  s   rs   c                 C   s   d|  dd d S )zQuotes an argument so that it will be interpreted literally by a POSIX
     shell. Taken from
     http://stackoverflow.com/questions/35817/whats-the-best-way-to-escape-ossystem-calls-in-python
     'z'\''replacesr:   r:   r;   EscapeShellArgumentZ  s   ry   c                 C   s   |  ddS )zrMake has its own variable expansion syntax using $. We must escape it for
     string to be interpreted literally.$z$$ru   rw   r:   r:   r;   EscapeMakeVariableExpansionb  s   r{   c                 C   s   t | } t| } | ddS )zBEscapes a CPP define so that it will reach the compiler unaltered.#z\#)ry   r{   rv   rw   r:   r:   r;   EscapeCppDefineh  s   r}   c                 C   s    d| v rd|  dd d } | S )zSTODO: Should this ideally be replaced with one or more of the above
     functions?"z\"ru   stringr:   r:   r;   QuoteIfNecessaryq  s   r   c                 C   s   t dd| S )zGConvert a string to a value that is acceptable as a make variable name.z[^a-zA-Z0-9_]_)resubr   r:   r:   r;   StringToMakefileVariabley  s   r   c                 C   s$   d| v r| S t j| r| S t|  S )z,Convert a path to its source directory form.$()rI   rJ   isabssrcdir_prefix)rJ   r:   r:   r;   	Sourceify  s
   r   \ c                 C   s   |  d|S )N ru   )rx   quoter:   r:   r;   QuoteSpaces  s   r   c           
      C   s   |  dddkr
dS i }|D ] }tj|\}}|dv }|sqtj|}||g | qd}| D ]\}}	t|	dkrJ|d|d	|	f 7 }q5|r_t
d	| d
  | d d  tddS )zMakes sure if duplicate basenames are not specified in the source list.

  Arguments:
    spec: The target dictionary containing the properties of the target.
  typeNstatic_library)r_   r`   ra   rb   r$   r%   rc   rd   r      z	  %s: %s
r   z<static library %s has several files with the same basename:
target_namezlibtool on OS X will generatez warnings for them.z6Duplicate basenames in sources section, see list above)rE   rI   rJ   rr   basenamer-   appenditemslenrL   printr   )
specall_sources	basenamessourcenamer[   is_compiled_filer   errorfilesr:   r:   r;   _ValidateSourcesForOSX  s6   r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d'efd(d)Z	&	*dCd+d,Z	&	&dDd-d.Zd/d0 ZdEd1d2ZdFd3d4Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#d&S )GMakefileWriterzMakefileWriter packages up the writing of one target-specific foobar.mk.

  Its only real entry point is Write(), and is mostly used for namespacing.
  c                 C   s   || _ || _i | _i | _i | _t D ],}| j|d|t| f i | j|d|t| f i | j|d|t| f i qd S )NzT$(obj).$(TOOLSET)/$(TARGET)/%%.o: $(srcdir)/%%%s FORCE_DO_CMD
	@$(call do_cmd,%s,1)
z\$(obj).$(TOOLSET)/$(TARGET)/%%.o: $(obj).$(TOOLSET)/%%%s FORCE_DO_CMD
	@$(call do_cmd,%s,1)
zQ$(obj).$(TOOLSET)/$(TARGET)/%%.o: $(obj)/%%%s FORCE_DO_CMD
	@$(call do_cmd,%s,1)
)r=   r7   suffix_rules_srcdirsuffix_rules_objdir1suffix_rules_objdir2r3   rU   r4   )selfr=   r7   r[   r:   r:   r;   __init__  s    






zMakefileWriter.__init__c                    sV  t j| t|d _ jt | _| _|d  _	|d  _
|d  _t j j| _ jdkr=t j| _nd _ |\}}g }	g }
g }g }g } jra | _ | _n	 |  _ _t|dd _d	 _ js j
 jv rtj j _  }n j _ j}  d
 j    d j	  d|v r !|d |
|	|| d|v r "|d |
|	|| d|v rΈ #|d |	|  jr|dg | } $||  %| |dg |
 }|rx jdkrt&||  '||||	||t j( j fdd j) dd |D }|rx  t* t+dd |D }|D ]}| j,v r:   j,|  q*  t- |D ]}| j.v rS   j.|  qC|D ]}| j/v rg   j/|  qW  d  jrx|0 j  1||||| ||	| |t2|<  j
dv r jt3|<  j4ddr 5 j	||  j6  dS )a  The main entry point: writes a .mk file for a single target.

    Arguments:
      qualified_target: target we're generating
      base_path: path relative to source root we're building in, used to resolve
                 target-relative paths
      output_filename: output .mk file name to write
      spec, configs: gyp info
      part_of_all: flag indicating this target is part of 'all'
    wr   r   toolsetr   Nstandalone_static_libraryr   )
executableloadable_moduleshared_libraryzTOOLSET := z
TARGET := actionsrulescopiesmac_bundle_resourcessourcesc                       t  | S re   r   
Absolutifypr   r:   r;   <lambda>3      z&MakefileWriter.Write.<locals>.<lambda>c                 S      g | ]}t |r|qS r:   )rn   rh   xr:   r:   r;   
<listcomp>5      z(MakefileWriter.Write.<locals>.<listcomp>c                 S   s   g | ]
}t j|d  qS )r   rq   )rh   rx   r:   r:   r;   r   8  s    z!# End of this set of suffix rules)r   r   r>   )7r*   r+   EnsureDirExistsopenfprX   headerqualified_targetrJ   targetr   r   xcode_emulationIsMacBundler7   is_mac_bundleXcodeSettingsxcode_settingsComputeDepsComputeMacBundleOutputoutputComputeMacBundleBinaryOutputoutput_binaryComputeOutputboolrE   is_standalone_static_library_INSTALLABLE_TARGETSrI   r   alias_InstallableTargetInstallPathWriteLnWriteActions
WriteRulesWriteCopiesWriteMacBundleResourcesWriteMacInfoPlistr   WriteSourcesMacPrefixHeaderPchify#SHARED_HEADER_SUFFIX_RULES_COMMENT1setr   #SHARED_HEADER_SUFFIX_RULES_COMMENT2r   r   r   WriteTargettarget_outputstarget_link_depsr=   WriteAndroidNdkModuleRuleclose)r   r   	base_pathoutput_filenamer   configspart_of_alldeps	link_depsextra_outputsextra_sourcesextra_link_depsextra_mac_bundle_resourcesmac_bundle_depsinstall_pathall_mac_bundle_resourcesr   r   rZ   r[   r:   r   r;   Write  s   












zMakefileWriter.Writec                 C   s   t j| t|d| _| jt | dtj	
tj	||  | d | d |r2d| }| d|d
|f  | j  dS )	a  Write a "sub-project" Makefile.

    This is a small, wrapper Makefile that calls the top-level Makefile to build
    the targets from a single gyp file (i.e. a sub-project).

    Arguments:
      output_filename: sub-project Makefile name to write
      makefile_path: path to the top-level Makefile
      targets: list of "all" targets for this sub-project
      build_dir: build output directory, relative to the sub-project
    r   zexport builddir_name ?= %sz.PHONY: allzall:z -C z	$(MAKE)%s %sr   N)r*   r+   r   r   r   rX   r   r   rI   rJ   rL   dirnamer   )r   r   makefile_pathtargets	build_dirr:   r:   r;   WriteSubMake[  s   

zMakefileWriter.WriteSubMakec                    s     |D ]9}tdj|d f }d|d   |d }|d }	t }
|	D ]}tj|d }|r;|
| q*t	|
ddrH||	7 }t	|
d	drT||	7 }|d
 }jdkrf fdd|D }tj|}d|v r|d||d f  n	d||f  t|
dkrdd|
 d | }dtjpd }|dj}|dj}d|||f    fdd|	D }	dt|	d   dt|	d   |	d    |D ]}d|vsJ d| q|	D ]}d|vsJ d| q fdd|	D }	 fdd|D }j|	tttj|||d d| }d |d|	f  |d!|    q  d"S )#a  Write Makefile code for any 'actions' from the gyp input.

    extra_sources: a list that will be filled in with newly generated source
                   files, if any
    extra_outputs: a list that will be filled in with any outputs of these
                   actions (used to make other pieces dependent on these
                   actions)
    part_of_all: flag indicating this target is part of 'all'
    %s_%saction_namez### Rules for action "%s":inputsoutputsr   process_outputs_as_sourcesF'process_outputs_as_mac_bundle_resourcesactionr   c                       g | ]	}t j| qS r:   r*   r   ExpandEnvVarsrh   commandenvr:   r;   r         z/MakefileWriter.WriteActions.<locals>.<listcomp>messagezquiet_cmd_%s = ACTION %s $@zmkdir -p %sr   z; cd %s; .	$(TARGET)ztcmd_%s = LD_LIBRARY_PATH=$(builddir)/lib.host:$(builddir)/lib.target:$$LD_LIBRARY_PATH; export LD_LIBRARY_PATH; %s%sc                       g | ]}  |qS r:   r   rh   or   r:   r;   r         %s: obj := $(abs_obj)%s: builddir := $(abs_builddir)z3Spaces in action input filenames not supported (%s)z4Spaces in action output filenames not supported (%s)c                    r   r:   r   r  r  r:   r;   r         c                    r   r:   r   rh   ir  r:   r;   r     r  )r   r  zaction_%s_outputsz%s := %s$(%s)N)GetSortedXcodeEnvr   r   r   r   rI   rJ   splitaddintrE   r7   r*   r+   EncodePOSIXShellListr   rL   r   rv   r   r   WriteSortedXcodeEnv
WriteDoCmdmapr   r   )r   r   r   r   r   r   r   r   r   r   dirsrA   diraction_commandsr  	cd_actioninputr   outputs_variabler:   )r  r   r;   r   v  st   



	

zMakefileWriter.WriteActionsc                    s     |D ]{}tdj|d f }d}d|  g }	|dg D ]?}
t }tj|
\}tj	|\}fdd|d D }|D ]}tj
|}|rY|| qJt|d	d
rf||7 }t|dd
rr||7 }tttj|
g|dg  }d||f g}|dkr|dg7 } fdd|D } fdd|D }fdd|D }|	|7 }	d|d   d|d   j|||d||f d td}|D ]}t|d|}d|vsJ d| q׈dd|  fdd|d D }d}t|dkrdd| }dtjpd  }jd!kr+ fd"d|D }tj|}|d#j}|d#j}|d#j}d$|||||d%  d&||d'    |d(7 }q%d)| }|	| |d*|  d+|    qd, d d-S ).a  Write Makefile code for any 'rules' from the gyp input.

    extra_sources: a list that will be filled in with newly generated source
                   files, if any
    extra_outputs: a list that will be filled in with any outputs of these
                   rules (used to make other pieces dependent on these rules)
    part_of_all: flag indicating this target is part of 'all'
    r   	rule_namer   z### Generated for rule %s:rule_sourcesc                       g | ]	} | qS r:   ExpandInputRoot)rh   rA   rule_source_dirnamerule_source_rootr   r:   r;   r     
    z-MakefileWriter.WriteRules.<locals>.<listcomp>r   r   Fr   r   z$(call do_cmd,%s_%d)resources_gritz@touch --no-create $@c                    r   r:   r   r  r  r:   r;   r     r  c                    r   r:   r   r  r  r:   r;   r     r  c                    r  r:   r  r  r   r:   r;   r     r  r  r  z%s_%d)r  z\$\([^ ]* \$<\)r   r   z/Spaces in rule filenames not yet supported (%s)all_deps += %sc                    r&  r:   r'  )rh   acr)  r:   r;   r   $  r,  r   zmkdir -p %s; r  r	  r   c                    r   r:   r   r  r  r:   r;   r   0  r  r
  zcmd_%(name)s_%(count)d = LD_LIBRARY_PATH=$(builddir)/lib.host:$(builddir)/lib.target:$$LD_LIBRARY_PATH; export LD_LIBRARY_PATH; %(cd_action)s%(mkdirs)s%(action)s)r   r!  countmkdirsr   z9quiet_cmd_%(name)s_%(count)d = RULE %(name)s_%(count)d $@)r0  r   r   zrule_%s_outputsr  z$### Finished generating for rule: %sz%### Finished generating for all rulesN)r  r   r   r   rE   r   rI   rJ   r  rr   r   r  r  r  r   r   WriteMakeRuler   compiler   rL   r   r7   r*   r+   r  rv   r   	WriteListr   )r   r   r   r   r   r   ruler   r0  all_outputsrule_sourcer  rule_source_basenamerule_source_extr   rA   r  r   r   variables_with_spacesr   r   r1  r!  r#  r:   )r  r*  r+  r   r;   r     s   













zMakefileWriter.WriteRulesc              	   C   s   |  d t| jd }g }|D ]G}|d D ]@}t| |}tj|d }t| tj|d |}	| 	 }
t
j|	|
}	t
j||
}| |	g|gd| ||	 qq|  d|dtt|f  |d	|  |    d
S )a  Write Makefile code for any 'copies' from the gyp input.

    extra_outputs: a list that will be filled in with any outputs of this action
                   (used to make other pieces dependent on this action)
    part_of_all: flag indicating this target is part of 'all'
    z### Generated for copy rule._copiesr   r   destinationcopyz%s = %sr   r  N)r   r   r   r   r   rI   rJ   r  rL   r  r*   r   r  r  r   r  r   )r   r   r   r   variabler   r=  rJ   rk   r   r  r:   r:   r;   r   Z  s&   
zMakefileWriter.WriteCopiesc              	   C   st   |  d tjtd | jttt| j|D ] \}}t	j
|\}}|dkr7| j|g|gddd || qdS )z0Writes Makefile code for 'mac_bundle_resources'.z&### Generated for mac_bundle_resourcesr   z	.xcassetszmac_tool,,,copy-bundle-resourceTr   N)r   r*   r   GetMacBundleResourcesr.   r   r  r   r   rI   rJ   rr   r  r   )r   	resourcesbundle_depsr   rm   r   r[   r:   r:   r;   r     s   


z&MakefileWriter.WriteMacBundleResourcesc                    s   t jtd  j fdd\}}}}|sdS |r9dtj| } j||d dt	d  
|g|gd	d
g |} | j|d  j|g|gddd || dS )z0Write Makefile code for bundle Info.plist files.r   c                    r   re   r   r   r   r:   r;   r     r   z2MakefileWriter.WriteMacInfoPlist.<locals>.<lambda>Nz$(obj).$(TOOLSET)/$(TARGET)/z: INFOPLIST_DEFINES-D)quoterz$(call do_cmd,infoplist)z@plutil -convert xml1 $@ $@additional_settingszmac_tool,,,copy-info-plistTr?  )r*   r   GetMacInfoPlistr.   r   rI   rJ   r   r4  r}   r2  r  r  r  r   )r   rB  
info_plistrA   defines	extra_envintermediate_plistr:   r   r;   r     s4   


z MakefileWriter.WriteMacInfoPlistc                    s  t | D ]}|| }	 j|	dd| dtd  jdkr> j|}
 j|} j	|} j
|} j|}n|	d}
|	d}|	d} d	  |
d
|   d  |d|   d  |d|   jdkr d  |d|   d  |d|  |	d}|r fdd|D } j|d| dd qtt|} fdd|D } |d |D ]}d|vsJ d| qĈ d  d    |r jdg|d d!d" |r jdg|d#d!d" |||}|r d$ |D ]\}}} d%||f  q d& |rd|d  d'  d(  d)|d* d+   d,|d- d.   jdkrd d/|d0 d1   d2|d3 d4   |  |d5d |D 7 }   d6S )7a  Write Makefile code for any 'sources' from the gyp input.
    These are source files necessary to build the current target.

    configs, deps, sources: input from gyp.
    extra_outputs: a list of extra outputs this action should be dependent on;
                   used to serialize action/rules before compilation
    extra_link_deps: a list that will be filled in with any outputs of
                     compilation (to be used in link lines)
    part_of_all: flag indicating this target is part of 'all'
    rI  zDEFS_%srC  prefixrD  r   cflagscflags_c	cflags_ccz## Flags passed to all source files.z	CFLAGS_%sz# Flags passed to only C files.zCFLAGS_C_%sz!# Flags passed to only C++ files.zCFLAGS_CC_%sz"# Flags passed to only ObjC files.zCFLAGS_OBJC_%sz$# Flags passed to only ObjC++ files.zCFLAGS_OBJCC_%sinclude_dirsc                    s   g | ]	}t  |qS r:   r   )rh   includer   r:   r;   r     r  z/MakefileWriter.WriteSources.<locals>.<listcomp>zINCS_%sz-I)rM  c                    s    g | ]}   t|qS r:   )	Objectifyr   rs   r   r   r:   r;   r     s     OBJSr   z-Spaces in object filenames not supported (%s)z?# Add to the list of files we specially track dependencies for.zall_deps += $(OBJS)z$(OBJS)z6Make sure our dependencies are built before any of us.Tcomment
order_onlyz1Make sure our actions/rules run before any of us.z:# Dependencies from obj files to their precompiled headers%s: %sz%# End precompiled header dependencieszn# CFLAGS et al overrides must be target-local.
# See "Target-specific Variable Values" in the GNU Make manual.z$(OBJS): TOOLSET := $(TOOLSET)zD$(OBJS): GYP_CFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) %s cz/$(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE))zF$(OBJS): GYP_CXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) %s r]   z0$(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE))zG$(OBJS): GYP_OBJCFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) %s mzK$(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE)) $(CFLAGS_OBJC_$(BUILDTYPE))zI$(OBJS): GYP_OBJCXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) %s mmzM$(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE)) $(CFLAGS_OBJCC_$(BUILDTYPE))c                 S   r   r:   )rp   )rh   r   r:   r:   r;   r   "  r   N)rT   rU   r4  rE   r}   r7   r   	GetCflags
GetCflagsCGetCflagsCCGetCflagsObjCGetCflagsObjCCr   filterrn   r2  GetObjDependenciesr   
GetIncludeWritePchTargetsGetPchBuildCommands)r   r   r   r   r   r   r   precompiled_header
confignameconfigrN  rO  rP  cflags_objccflags_objccincludes
compilableobjsobjpchdepsr   gchr:   r   r;   r     s   





















zMakefileWriter.WriteSourcesc                 C   s   |sdS |D ]Q\}}}}ddddd| }ddd	d
d| }|  d|||f d |  |  d||f  |  d|  |  d d|vsKJ d| |  d|  |  d qdS )z,Writes make rules to compile prefix headers.Nz$(CFLAGS_C_$(BUILDTYPE))z$(CFLAGS_CC_$(BUILDTYPE))z4$(CFLAGS_C_$(BUILDTYPE)) $(CFLAGS_OBJC_$(BUILDTYPE))z6$(CFLAGS_CC_$(BUILDTYPE)) $(CFLAGS_OBJCC_$(BUILDTYPE)))rY  r]   rZ  r[  GYP_PCH_CFLAGSGYP_PCH_CXXFLAGSGYP_PCH_OBJCFLAGSGYP_PCH_OBJCXXFLAGSz%s: %s := %s zA$(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) z%s: %s FORCE_DO_CMDz	@$(call do_cmd,pch_%s,1)r   r   z*Spaces in gch filenames not supported (%s)r.  )r   )r   pch_commandsrp  	lang_flaglangr"  extra_flagsvar_namer:   r:   r;   rd  &  s>   

zMakefileWriter.WritePchTargetsc                 C   s  | j rJ | jdkr| jdv r| j S |d }d}d}| jdkr4|dd dkr/|dd }d}d	}n9| jd
v rT|dd dkrG|dd }d}| jdkrQd	}nd}n| jdkr^d| }n| jdkrmtdd| jd|f |d|}|d|}|d}|rd| }|| | S )zReturn the 'output basename' of a gyp spec.

    E.g., the loadable module 'foobar' in directory 'baz' will produce
      'libfoobar.so'
    r   r   r   r   r   r   r   r   N   r   r   )r   r   r(   r)   nonez%s.stampr   z,ERROR: What output file should be generated?r   r   product_prefixproduct_nameproduct_extensionr	  )r   r7   r   r   GetExecutablePathr   rE   )r   r   r   target_prefix
target_extproduct_extr:   r:   r;   ComputeOutputBasenameG  s<   









z$MakefileWriter.ComputeOutputBasenamec                 C   s   | j dko| jdko| jdv S )Nr   r   rz  )r   r7   r   r   r:   r:   r;   _InstallImmediatelyr  s   z"MakefileWriter._InstallImmediatelyc                 C   sV   | j rJ tjd| j | j}| jdks|  rd}|d|}tj|| |S )zReturn the 'output' (full output path) of a gyp spec.

    E.g., the loadable module 'foobar' in directory 'baz' will produce
      '$(obj)/baz/libfoobar.so'
    z$(obj).r   r   product_dir)	r   rI   rJ   rL   r   r   r  rE   r  r   r   rJ   r:   r:   r;   r   w  s   
zMakefileWriter.ComputeOutputc                 C   s&   | j sJ td }tj|| j S )zDReturn the 'output' (full output path) to a bundle output directory.r   )r   r.   rI   rJ   rL   r   GetWrapperNamer  r:   r:   r;   r     s   
z%MakefileWriter.ComputeMacBundleOutputc                 C   s   t d }tj|| j S )zAReturn the 'output' (full output path) to the binary in a bundle.r   )r.   rI   rJ   rL   r   r  r  r:   r:   r;   r     s   z+MakefileWriter.ComputeMacBundleBinaryOutputc                 C   sn   g }g }d|v r+| dd |d D  |d D ]}|tv r%|t|  q| | tj|tj|fS )zCompute the dependencies of a gyp spec.

    Returns a tuple (deps, link_deps), where each is a list of
    filenames that will need to be put in front of make for either
    building (deps) or linking (link_deps).
    dependenciesc                 S   s   g | ]
}t | rt | qS r:   )r   rh   depr:   r:   r;   r     s    z.MakefileWriter.ComputeDeps.<locals>.<listcomp>)extendr   r   r*   r+   uniquer)r   r   r   r   r  r:   r:   r;   r     s   
zMakefileWriter.ComputeDepsc                 C   s   | j | jg|ddd d S )Nz Build our special outputs first.TrU  )r2  r   )r   r   r   r:   r:   r;   WriteDependencyOnExtraOutputs  s   
z,MakefileWriter.WriteDependencyOnExtraOutputsc                    s(    d |r  j|  j||ddd i } jdkrt| D ]}	||	 }
 jdkrk j	|	t
d  fdd	}tj j} j|	ttjtj| jttjtj| j}|rj|||	< n|
d
g }tdd |D r|d j  |d j  |
dg }|dd |D 7 } |d|	   jdkr  j|	d|	  q$|d}|rtj|} jdkrˈ j|} |d   dt j    dt j   jdkr  dt j  g } jdkr|r|d |tj| |rn   j !  |D ]}	  dt j|	tj"||	 f  q|#dtj"d jg t$|D ]\}}|%dsSt&|||< qC  dt j    dt jd |f   j'r  j|  t(t|d!   d"t j   jd#v r  d$ j)   |r  d% g }  d&   d't j  |r̈ j'rJ d( j* d)|vsJ d* jd+kr
  d,t jd t(t|f   jd-kr jd.kr j+ jg|d/||d0 nň j+ jg|d1||d0 n jd2krE|D ]}d |vsJ d3| q jd4vr8 j,s8 j+ jg|d5||d0 n j+ jg|d6||d0 n} jd7krj  d,t jd t(t|f   j+ jg|d8||d0 nX jd9kr|D ]}d |vsJ d:| qr jd-kr jd.kr j+ jg|d;||d0 n( j+ jg|d<||d0 n jdkr j+ jg|d=||d0 nt-d> j j*  jr j j*kr j j.vr j j*g jgd?dd@ |r jdAg j*gdBdd@  j j.v s j,r jd7krdC}n jd2krdD}nd+} / } jg} jdkr7d)|vr7 jdEkr7| jks7J dF| jf  j j*g|gd?dd@ | jkrc j'rOJ  j+|g jgdGdH| |dI ||  j j0kr~ j0 j*kr~ j j0g|dJ| dd@ |r jdAg|gdK| dd@ dLS dLS dLS )Ma&  Write Makefile code to produce the final target of the gyp spec.

    spec, configs: input from gyp.
    deps, link_deps: dependency lists; see ComputeDeps()
    extra_outputs: any extra outputs that our target should depend on
    part_of_all: flag indicating this target is part of 'all'
    z### Rules for final target.z4Preserve order dependency of special output on deps.TrU  r|  r   r   c                    r   re   r   r   r   r:   r;   r     r   z,MakefileWriter.WriteTarget.<locals>.<lambda>ldflagsc                 s   s"    | ]}| d pd|v V  qdS )r)   z.so.Nrf   r  r:   r:   r;   rl     s     z-MakefileWriter.WriteTarget.<locals>.<genexpr>z-Wl,-rpath=\$$ORIGIN/lib.%s/z$-Wl,-rpath-link=\$(builddir)/lib.%s/library_dirsc                 S   s   g | ]}d | qS )z-L%sr:   )rh   library_dirr:   r:   r;   r         z.MakefileWriter.WriteTarget.<locals>.<listcomp>z
LDFLAGS_%szLIBTOOLFLAGS_%s	librariesLIBSz*%s: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))z%s: LIBS := $(LIBS)z4%s: GYP_LIBTOOLFLAGS := $(LIBTOOLFLAGS_$(BUILDTYPE))z!$(TARGET_POSTBUILDS_$(BUILDTYPE))z%s: TARGET_POSTBUILDS_%s := %sr   cdrz   r  z%s: POSTBUILDS := %sr   BUNDLE_DEPSz%s: $(BUNDLE_DEPS))r   r   z+	@$(call do_cmd,mac_package_framework,,,%s)z	@$(call do_postbuilds)z	@true  # No-op, used by testsz	@touch -c %szQPostbuilds for bundles should be done on the bundle, not the binary (target '%s')r  z.Postbuilds do not work with custom product_dirr   z%s: LD_INPUTS := %shostr&   	link_host)
postbuildslinkr   z2Spaces in alink input filenames not supported (%s))r   openbsdnetbsdwin
alink_thinalinkr   solinkr   z3Spaces in module input filenames not supported (%s)solink_module_hostsolink_moduletouchzWARNING: no output forzAdd target alias)rV  phonyallz!Add target alias to "all" target.zshared libraryzstatic libraryr   z%s != %sr=  z Copy this to the %s output path.)rV  r   z!Short alias for building this %s.zAdd %s to "all" target.N)1r   r  r   r2  r   rT   rU   r7   r   
GetLdflagsr.   r*   r+   InvertRelativePathrJ   AddImplicitPostbuildsr   rI   rK   rL   r   rE   anyr   r   r4  GetLibtoolflagsr  AdjustLibrariesr  r   GetSpecPostbuildCommandsr  GetSortedXcodePostbuildEnvr  insert	enumerate
startswithry   r   r  GetFrameworkVersionr   r  r   r   r   r   r   )r   r   r   r   r   rB  r   r   target_postbuildsrg  rh  r  gyp_to_buildtarget_postbuildr  r  r  r  	postbuildlink_dep	file_descr   installable_depsr:   r   r;   r     s  















zMakefileWriter.WriteTargetNr   c                    sB   d}|r fdd|D }dd | }| jd||f  dS )zWrite a variable definition that is a list of values.

    E.g. WriteList(['a','b'], 'foo', prefix='blah') writes out
         foo = blaha blahb
    but in a pretty-printed style.
    r   c                    s   g | ]} | qS r:   r:   )rh   lrL  r:   r;   r     r   z,MakefileWriter.WriteList.<locals>.<listcomp>z \
	z	%s :=%s

N)rL   r   rX   )r   
value_listr>  rM  rD  valuesr:   rL  r;   r4    s
   zMakefileWriter.WriteListFc                 C   s^   d}|rd|vs
J d}| j ||d||f g||dd dd |D }| d	d
|  dS )zWrite a Makefile rule that uses do_cmd.

    This makes the outputs dependent on the command line that was run,
    as well as support the V= make command line flag.
    r   ,z,,1z$(call do_cmd,%s%s)T)r   rV  r  forcec                 S   s   g | ]}t |tqS r:   )r   SPACE_REPLACEMENTr  r:   r:   r;   r     r  z-MakefileWriter.WriteDoCmd.<locals>.<listcomp>r.  r   N)r2  r   rL   )r   r   r   r  r   rV  r  suffixr:   r:   r;   r    s   	zMakefileWriter.WriteDoCmdc	                 C   sf  dd |D }t t|}|r| d|  |r!| dd|  |r,| d|d   |r0dnd	}	|rE| d
d|d||	f  nZt|dkr[| d|d d||	f  nD|r_|n| jd}
t|
	 }d| }| dd||f  | dd  | dd|f  | d|d||	f  |
dd |r|D ]	}| d|  q|   dS )a  Write a Makefile rule, with some extra tricks.

    outputs: a list of outputs for the rule (note: this is not directly
             supported by make; see comments below)
    inputs: a list of inputs for the rule
    actions: a list of shell commands to run for the rule
    comment: a comment to put in the Makefile above the rule (also useful
             for making this Python script's code self-documenting)
    order_only: if true, makes the dependency order-only
    force: if true, include FORCE_DO_CMD as an order-only dep
    phony: if true, the rule does not actually generate the named output, the
           output is just a name to run the rule
    command: (optional) command name to generate unambiguous labels
    c                 S   s   g | ]}t |qS r:   )r   r  r:   r:   r;   r     r  z0MakefileWriter.WriteMakeRule.<locals>.<listcomp>z# z.PHONY: r   z%s: TOOLSET := $(TOOLSET)r   z FORCE_DO_CMDr   z
%s: | %s%sr   z%s: %s%szutf-8z%s.intermediaterX  z	%sz@:z.INTERMEDIATEz$(call do_cmd,touch)N)r  r   r   rL   r   r   encodehashlibsha1	hexdigestr  )r   r   r   r   rV  rW  r  r  r  force_append	cmdstring	cmddigestintermediater   r:   r:   r;   r2    s:   
 zMakefileWriter.WriteMakeRulec                 C   sx  | j dvrdS | d | d | d|  | d | d | d | d	 d
d
d
d}d}|D ] }tj|d }||v rV||  d7  < || || krV|}q6| d|  | t| jtt	|d dd }ddi}	i }
t
|
|	 | ||td |
d d | ||td td d | j dkr| d n| j dkr| d n
| j dkr| d |   dS ) a  Write a set of LOCAL_XXX definitions for Android NDK.

    These variable definitions will be used by Android NDK but do nothing for
    non-Android applications.

    Arguments:
      module_name: Android NDK module name, which must be unique among all
          module names.
      all_sources: A list of source files (will be filtered by Compilable).
      link_deps: A list of link dependencies, which must be sorted in
          the order from dependencies to dependents.
    )r   r   r   Nz/# Variable definitions for Android applicationszinclude $(CLEAR_VARS)zLOCAL_MODULE := ziLOCAL_CFLAGS := $(CFLAGS_$(BUILDTYPE)) $(DEFS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE)) $(INCS_$(BUILDTYPE))z+LOCAL_CPPFLAGS := $(CFLAGS_CC_$(BUILDTYPE))zLOCAL_C_INCLUDES :=z/LOCAL_LDLIBS := $(LDFLAGS_$(BUILDTYPE)) $(LIBS)r   )r`   ra   rb   ra   r   zLOCAL_CPP_EXTENSION := LOCAL_SRC_FILESc                 S   sN   g }| D ] }t j|}||r$||r$||t|t|   q|S re   )rI   rJ   r   r  rg   r   r   )r   rM  r  modulesfilepathrk   r:   r:   r;   DepsToModules2  s   z?MakefileWriter.WriteAndroidNdkModuleRule.<locals>.DepsToModulesr7   r'   r   r   LOCAL_SHARED_LIBRARIESr   r   LOCAL_STATIC_LIBRARIESr   zinclude $(BUILD_EXECUTABLE)r   zinclude $(BUILD_SHARED_LIBRARY)r   zinclude $(BUILD_STATIC_LIBRARY))r   r   rI   rJ   rr   r4  r  r   ra  rn   r<   r.   )r   module_namer   r   cpp_extdefault_cpp_extrk   r[   r  r6   r5   r:   r:   r;   r      s^   






	




z(MakefileWriter.WriteAndroidNdkModuleRulec                 C   s   | j |d  d S )NrS   )r   rX   )r   textr:   r:   r;   r   S  s   zMakefileWriter.WriteLnc                 C   s"   t j| jdtjd| jd|S )Nz$(abs_builddir)z$(abs_srcdir)r   )r*   r   r  r   rI   rJ   rL   )r   rF  r:   r:   r;   r  W  s
   z MakefileWriter.GetSortedXcodeEnvc                 C   s   | j dd}| jd|idS )NCHROMIUM_STRIP_SAVE_FILEr   rE  )r   GetPerTargetSettingr  )r   strip_save_filer:   r:   r;   r  ^  s   z)MakefileWriter.GetSortedXcodePostbuildEnvc                 C   s*   |D ]\}}|  dt|||f  qd S )Nz%s: export %s := %s)r   r   )r   r   r  kvr:   r:   r;   r  i  s   z"MakefileWriter.WriteSortedXcodeEnvc                 C   s4   d|v r| dd| j }d|vrd| j|f }|S )z,Convert a path to its output directory form.r   $(obj)/z$(obj).%s/$(TARGET)/z$(obj)z$(obj).%s/$(TARGET)/%s)rv   r   r   rJ   r:   r:   r;   rS  t  s
   zMakefileWriter.Objectifyc                 C   s<   |  |}d|v r|dd| j|f }|S d| j||f S )z:Convert a prefix header path to its output directory form.r   r  z$(obj).%s/$(TARGET)/pch-%sz$(obj).%s/$(TARGET)/pch-%s/%s)r   rv   r   )r   rJ   rw  r:   r:   r;   r   }  s   
zMakefileWriter.Pchifyc                 C   s*   d|v r	| dS tjtj| j|S )zlConvert a subdirectory-relative path into a base-relative path.
    Skips over paths that contain variables.r   /)rstriprI   rJ   rK   rL   r  r:   r:   r;   r     s   
zMakefileWriter.Absolutifyc                 C   s&   d|vr
d|vr
|S |||d }|S )Nr	   r
   )
INPUT_ROOTINPUT_DIRNAMEr:   )r   template	expansionr   rJ   r:   r:   r;   r(    s   zMakefileWriter.ExpandInputRootc                 C   s8   | j dkr| jdks| jdkrd| j| jf S d| j S )zCReturns the location of the final output for an installable target.r   r   r   z$(builddir)/lib.%s/%sz$(builddir)/)r   r7   r   r   r   r:   r:   r;   r     s   

z,MakefileWriter._InstallableTargetInstallPath)NF)NNFFFN)r   re   )$__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   rd  r  r  r   r   r   r   r  r   r   r4  r  r2  r   r   r  r  r  rS  r   r   r(  r   r:   r:   r:   r;   r     sT     d &|!+ m


>
S
	

r   c                    s   | d   fdd| d D }t j| d  j}|tjs&tjd|}|	d|dt
t|t j|d	gt   | d
  dS )z,Write the target to regenerate the Makefile.r?   c                    s   g | ]
}t j| jqS r:   )r*   r+   RelativePathrH   )rh   rk   r?   r:   r;   r     s    z-WriteAutoRegenerationRule.<locals>.<listcomp>build_files_arg
gyp_binaryr	  zquiet_cmd_regen_makefile = ACTION Regenerating $@
cmd_regen_makefile = cd $(srcdir); %(cmd)s
%(makefile_name)s: %(deps)s
	$(call do_cmd,regen_makefile)

r   z-fmake)makefile_namer   cmdN)r*   r+   FixIfRelativePathrH   r  rI   seprJ   rL   rX   r  r   r  RegenerateFlags)r6   root_makefiler  build_filesbuild_files_argsr  r:   r  r;   WriteAutoRegenerationRule  s.   
r  c                 C   sb   |d }|D ](}dg}|j r|j dkr|d|j f7 }|d|  td||f  t| qd S )Nr?   maker	  z-Cz
BUILDTYPE=zBuilding [%s]: %s)rH   r   r   
subprocess
check_call)dataconfigurationsr6   r?   rh  	argumentsr:   r:   r;   PerformBuild  s   r  c           1         s|  |d t j|}|di }|dd}|dd }|dd}fdd	}	d }
tfd
d| D }| D ]}| }|d dkrI|d }
 nq7|
sNd}
d}dj }tjj	|}j
rttjj	j
|}t j|j
}dad}d}d}|||
|dtd|||d
}|dkrd}||dttd nI|dkr|dti n=|dkrd}d}|||td  n+|d!kr|d"dd# n|d$krd%}|d&|i n|d'krd%}||td"dd( |td)d*td+d,td-d.td/d0td1d2td3d4td5d6td7d8d9 t j| d: \ }}|  d;g }i }|D ]\}}|d<r5d=| ||d td<  < qd}|D ]\\}}td>|rHq;|d: d?krSd=| }||}|rdd@||f }||= |dAv r|dB| 7 }|ddC}|tjv rtj| }|dD||f 7 }|dE7 }q;|dF||f 7 }q;||d;< t j| t|dG}|t|  |r|dH |D ]}|dI|  t | qtj!|}t j"|| t |dJ D ] t j#|  D ]}$| qqt } t }!| D ]}"t j|"\ }}|  d;g }#||#ksJ dK|#|f | $t j j	 |  dL }$|$D ]/}%t jt j%|% j	}&tj&|&}'|dM rT|''|dM rT| $|' q+| $|& q+|	 |d | j dN \}(})|" }|dO }*|dkrt j()|  | t*||}+|+j+|"|(|)||*|"v dP t j|)tj!|},|!$|, qt jj,t- }-| D ]D tj|-   fdQd| D }.|.sʐq|	 tj.tj/ d: dR \}(})t jtj!|tj!|)}/|+0|)|/|.| q|dS t1|!D ]}0|dT|0 dU  |dV|0 dS  |dE q|dS |dWs3|dXdYr3t2||||  |t3 |4  d S )ZNr?   r=   r@   rA   r>   default_targetr  c                    sf   t jtj|  j}tj j||} jr#tj j j||}t jtj|  j	}||fS )z9Determine where to write a Makefile for a given gyp file.)
r*   r+   r  rI   rJ   r   depthrL   rG   rH   )
build_file	base_namer   output_filer  r:   r;   CalculateMakefilePath  s   z-GenerateOutput.<locals>.CalculateMakefilePathc                    s   g | ]} | d  qS )r   r:   rh   r   )target_dictsr:   r;   r     r   z"GenerateOutput.<locals>.<listcomp>default_configurationDefaultr	  Makefilez
$(srcdir)/flockz-afz-MMDr   r   )
r  builddirr  r  flock_indexlink_commandsextra_commandssrcdircopy_archive_argsmakedep_argsr   z./gyp-mac-tool flock   )r  r  r  r  r&   r  zosz-fPRz-qmakedep=gcc)r  r	  r  solarisz./gyp-flock-tool flock)r  r  r  z-pPRfr  r(   )r  r  r  r  )	CC_targetCCz$(CC))	AR_targetARz$(AR))
CXX_targetCXXz$(CXX))LINK_targetLINKz$(LINK))CC_hostr  gcc)AR_hostr  ar)CXX_hostr  zg++)	LINK_hostr  z$(CXX.host))z	CC.targetz	AR.targetz
CXX.targetzLINK.targetCC.hostzAR.hostCXX.hostz	LINK.hostr   make_global_settings_wrapperz$(abspath %s)z
.*_wrapperrz   z%s %s)r  r  r  r  z3ifneq (,$(filter $(origin %s), undefined default))
r   z
  %s = %s
zendif
z	%s ?= %s
r   zU# Define LOCAL_PATH for build of Android applications.
LOCAL_PATH := $(call my-dir)

zTOOLSET := %s
r  zDmake_global_settings needs to be the same for all targets. %s vs. %sincluded_fileshome_dot_gypz.mkr  r?  c                    s*   g | ]}|  r|v r| d  qS )r   )r  r  )r  needed_targetsr  r:   r;   r     s    
z	.MakefilerS   zmifeq ($(strip $(foreach prefix,$(NO_LOAD),\
    $(findstring $(join ^,$(prefix)),\
                 $(join ^,z)))),)
z
  include 
standaloneauto_regenerationT)5r*   r+   r,   rE   r   r  rI   rJ   rL   rH   rG   r  r   LINK_COMMANDS_LINUXr4   LINK_COMMANDS_MACSHARED_HEADER_MAC_COMMANDSLINK_COMMANDS_ANDROIDLINK_COMMANDS_OS390LINK_COMMANDS_AIXr   ParseQualifiedTargetrg   r   r   matchrv   environr   r   rX   SHARED_HEADERr\   r   CopyTool
AllTargetsr  UnrelativePathabspathr  r   MergeGlobalXcodeSettingsToSpecr   r   r  getcwdrr   r   r   rT   r  SHARED_FOOTERr   )1target_listr  r  r6   r7   r=   rN   r>   r  r  r  toolsetsr   r   r  r  r   flock_commandcopy_archive_argumentsmakedep_argumentsheader_paramsr   make_global_settings_arraywrappersrQ   valuer  wrapperenv_keyr  r   	dest_pathr  include_listr   this_make_global_settingsr  included_filerelative_include_fileabs_include_filer   r  r   rY   mkfile_rel_pathdepth_rel_pathgyp_targetsmakefile_rel_pathinclude_filer:   )r  r!  r?   r  r;   GenerateOutput  s  




	














rK  )r   )6
__future__r   rI   r   sysr  r*   
gyp.commongyp.xcode_emulationr   r   r  r.   $generator_supports_multiple_toolsetsrF   r   r    r!   rM   r<   rO   r  r$  r%  r'  r)  r(  r-  r&  r\   r   r   r4  r   r3   rn   rp   rs   ry   r{   r}   r   r   r   r   r   r   r   r   objectr   r  r  rK  r:   r:   r:   r;   <module>   s   %!$HHII )  *  J  J  K  L  o		
	          |