Skip to content

Commit

Permalink
Introduce static_to_shared_library function
Browse files Browse the repository at this point in the history
The static_to_shared_library function takes an existing static library
and produces a shared library based on it.
  • Loading branch information
michaelkuhn committed Nov 2, 2017
1 parent 0d624ea commit 86a8838
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 73 deletions.
64 changes: 0 additions & 64 deletions var/spack/repos/builtin/packages/lua/liblua-shared.patch

This file was deleted.

61 changes: 52 additions & 9 deletions var/spack/repos/builtin/packages/lua/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,55 @@
import os


def static_to_shared_library(static_lib, shared_lib=None, **kwargs):
"""
Converts a static library to a shared library.
Parameters:
shared_lib (str): Path to the shared library.
static_lib (str): Path to the static library.
Keyword arguments:
compiler (str): Path to the compiler. Default is spack_cc.
libraries (str list): One or more libraries to link against.
"""
import platform

arch = platform.system().lower()

compiler_path = kwargs.get('compiler', spack_cc)
libraries = kwargs.get('libraries', [])

if not shared_lib:
shared_lib = '{0}.{1}'.format(os.path.splitext(static_lib)[0],
dso_suffix)

compiler = which(compiler_path)
compiler_args = []

if 'linux' in arch:
compiler_args = [
'-shared',
'-Wl,-soname,{0}'.format(os.path.basename(shared_lib)),
'-Wl,--whole-archive',
static_lib,
'-Wl,--no-whole-archive'
]
elif 'darwin' in arch:
compiler_args = [
'-dynamiclib',
'-install_name {0}'.format(shared_lib),
'-Wl,-force_load,{0}'.format(static_lib)
]

if len(libraries) > 0:
compiler_args.extend(libraries)

compiler_args.extend(['-o', shared_lib])

compiler(*compiler_args)


class Lua(Package):
"""The Lua programming language interpreter and library."""

Expand Down Expand Up @@ -58,11 +107,6 @@ class Lua(Package):
destination="luarocks",
placement='luarocks')

# Based on patches from Arch Linux and Homebrew:
# https://git.archlinux.org/svntogit/packages.git/tree/trunk/liblua.so.patch?h=packages/lua
# https://github.com/Homebrew/homebrew-core/blob/master/Formula/lua.rb
patch('liblua-shared.patch')

def install(self, spec, prefix):
if spec.satisfies("platform=darwin"):
target = 'macosx'
Expand All @@ -76,8 +120,6 @@ def install(self, spec, prefix):
spec['ncurses'].prefix.lib),
'MYLIBS=-lncursesw',
'CC=%s -std=gnu99' % spack_cc,
'LUA_DSO=liblua.%s' % (
dso_suffix),
target)
make('INSTALL_TOP=%s' % prefix,
'MYCFLAGS=%s' % (
Expand All @@ -87,10 +129,11 @@ def install(self, spec, prefix):
spec['ncurses'].prefix.lib),
'MYLIBS=-lncursesw',
'CC=%s -std=gnu99' % spack_cc,
'LUA_DSO=liblua.%s' % (
dso_suffix),
'install')

static_to_shared_library(join_path(prefix.lib, 'liblua.a'),
libraries=['-lm'])

with working_dir(os.path.join('luarocks', 'luarocks')):
configure('--prefix=' + prefix, '--with-lua=' + prefix)
make('build')
Expand Down

0 comments on commit 86a8838

Please sign in to comment.