Skip to content

Commit

Permalink
feat: support building shared libraries on z/OS (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcfyung committed Apr 4, 2022
1 parent 181f2e2 commit 293bcfa
Showing 1 changed file with 71 additions and 11 deletions.
82 changes: 71 additions & 11 deletions pylib/gyp/generator/make.py
Expand Up @@ -99,6 +99,8 @@ def CalculateVariables(default_variables, params):
default_variables.setdefault("OS", operating_system)
if flavor == "aix":
default_variables.setdefault("SHARED_LIB_SUFFIX", ".a")
elif flavor == "zos":
default_variables.setdefault("SHARED_LIB_SUFFIX", ".x")
else:
default_variables.setdefault("SHARED_LIB_SUFFIX", ".so")
default_variables.setdefault("SHARED_LIB_DIR", "$(builddir)/lib.$(TOOLSET)")
Expand Down Expand Up @@ -266,10 +268,10 @@ def CalculateGeneratorInputInfo(params):
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
cmd_solink = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,DLL -o $(patsubst %.x,%.so,$@) $(LD_INPUTS) $(LIBS) && if [ -f $(notdir $@) ]; then /bin/cp $(notdir $@) $@; else true; fi
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
cmd_solink_module = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)
""" # noqa: E501


Expand Down Expand Up @@ -418,6 +420,9 @@ def CalculateGeneratorInputInfo(params):
# 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 "$<" "$@")
quiet_cmd_symlink = SYMLINK $@
cmd_symlink = ln -sf "$<" "$@"
%(link_commands)s
""" # noqa: E501
r"""
Expand Down Expand Up @@ -999,12 +1004,20 @@ def WriteActions(
# libraries, but until everything is made cross-compile safe, also use
# target libraries.
# TODO(piman): when everything is cross-compile safe, remove lib.target
self.WriteLn(
"cmd_%s = LD_LIBRARY_PATH=$(builddir)/lib.host:"
"$(builddir)/lib.target:$$LD_LIBRARY_PATH; "
"export LD_LIBRARY_PATH; "
"%s%s" % (name, cd_action, command)
)
if self.flavor == "zos" or self.flavor == "aix":
self.WriteLn(
"cmd_%s = LIBPATH=$(builddir)/lib.host:"
"$(builddir)/lib.target:$$LIBPATH; "
"export LIBPATH; "
"%s%s" % (name, cd_action, command)
)
else:
self.WriteLn(
"cmd_%s = LD_LIBRARY_PATH=$(builddir)/lib.host:"
"$(builddir)/lib.target:$$LD_LIBRARY_PATH; "
"export LD_LIBRARY_PATH; "
"%s%s" % (name, cd_action, command)
)
self.WriteLn()
outputs = [self.Absolutify(o) for o in outputs]
# The makefile rules are all relative to the top dir, but the gyp actions
Expand Down Expand Up @@ -1498,6 +1511,8 @@ def ComputeOutputBasename(self, spec):
target_prefix = "lib"
if self.flavor == "aix":
target_ext = ".a"
elif self.flavor == "zos":
target_ext = ".x"
else:
target_ext = ".so"
elif self.type == "none":
Expand Down Expand Up @@ -1578,6 +1593,14 @@ def ComputeDeps(self, spec):
# link_deps.extend(spec.get('libraries', []))
return (gyp.common.uniquer(deps), gyp.common.uniquer(link_deps))

def GetSharedObjectFromSidedeck(self, sidedeck):
"""Return the shared object files based on sidedeck"""
return re.sub(r"\.x$", ".so", sidedeck)

def GetUnversionedSidedeckFromSidedeck(self, sidedeck):
"""Return the shared object files based on sidedeck"""
return re.sub(r"\.\d+\.x$", ".x", sidedeck)

def WriteDependencyOnExtraOutputs(self, target, extra_outputs):
self.WriteMakeRule(
[self.output_binary],
Expand Down Expand Up @@ -1816,6 +1839,11 @@ def WriteTarget(
part_of_all,
postbuilds=postbuilds,
)
# z/OS has a .so target as well as a sidedeck .x target
if self.flavor == "zos":
self.WriteLn('%s: %s' % (
QuoteSpaces(self.GetSharedObjectFromSidedeck(self.output_binary)),
QuoteSpaces(self.output_binary)))
elif self.type == "loadable_module":
for link_dep in link_deps:
assert " " not in link_dep, (
Expand Down Expand Up @@ -1873,7 +1901,9 @@ def WriteTarget(
else:
file_desc = "executable"
install_path = self._InstallableTargetInstallPath()
installable_deps = [self.output]
installable_deps = []
if self.flavor != "zos":
installable_deps.append(self.output)
if (
self.flavor == "mac"
and "product_dir" not in spec
Expand All @@ -1898,15 +1928,42 @@ def WriteTarget(
comment="Copy this to the %s output path." % file_desc,
part_of_all=part_of_all,
)
installable_deps.append(install_path)
if self.flavor != "zos":
installable_deps.append(install_path)
if self.flavor == 'zos' and self.type == 'shared_library':
# lib.target/libnode.so has a dependency on $(obj).target/libnode.so
self.WriteDoCmd([self.GetSharedObjectFromSidedeck(install_path)],
[self.GetSharedObjectFromSidedeck(self.output)], 'copy',
comment='Copy this to the %s output path.' %
file_desc, part_of_all=part_of_all)
# Create a symlink of libnode.x to libnode.version.x
self.WriteDoCmd([self.GetUnversionedSidedeckFromSidedeck(install_path)],
[install_path], 'symlink',
comment='Symlnk this to the %s output path.' %
file_desc, part_of_all=part_of_all)
# Place libnode.version.so and libnode.x symlink in lib.target dir
installable_deps.append(self.GetSharedObjectFromSidedeck(install_path))
installable_deps.append(
self.GetUnversionedSidedeckFromSidedeck(install_path))
if self.output != self.alias and self.alias != self.target:
self.WriteMakeRule(
[self.alias],
installable_deps,
comment="Short alias for building this %s." % file_desc,
phony=True,
)
if part_of_all:
if self.flavor == 'zos' and self.type == 'shared_library':
# Make sure that .x symlink target is run
self.WriteMakeRule(
['all'],
[
self.GetUnversionedSidedeckFromSidedeck(install_path),
self.GetSharedObjectFromSidedeck(install_path)
],
comment='Add %s to "all" target.' % file_desc,
phony=True,
)
elif part_of_all:
self.WriteMakeRule(
["all"],
[install_path],
Expand Down Expand Up @@ -2202,6 +2259,9 @@ def _InstallableTargetInstallPath(self):
# # Install all shared libs into a common directory (per toolset) for
# # convenient access with LD_LIBRARY_PATH.
# return "$(builddir)/lib.%s/%s" % (self.toolset, self.alias)
if self.flavor == "zos" and self.type == "shared_library":
return "$(builddir)/lib.%s/%s" % (self.toolset, self.alias)

return "$(builddir)/" + self.alias


Expand Down

0 comments on commit 293bcfa

Please sign in to comment.