Skip to content

Instantly share code, notes, and snippets.

@udnaan
Created October 20, 2018 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udnaan/d549950a33fd82d13f9e6ba4aae82964 to your computer and use it in GitHub Desktop.
Save udnaan/d549950a33fd82d13f9e6ba4aae82964 to your computer and use it in GitHub Desktop.
Use distutils to create a static library (for external usage)
from distutils.ccompiler import new_compiler
from sysconfig import get_paths
import os
project_name = "slimey_project"
source = ['source1.c']
include_dirs = ['include']
build_dir = os.path.join(os.path.dirname(__file__), 'build')
class StaticLib(Command):
description = 'build static lib'
user_options = [] # do not remove, needs to be stubbed out!
python_info = get_paths()
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Create compiler with default options
c = new_compiler()
# Optionally add include directories etc.
for d in include_dirs:
c.add_include_dir(d)
c.add_include_dir(self.python_info['include'])
# Compile into .o files
objects = c.compile(sources)
# Create static or shared library
c.create_static_lib(objects, project_name, output_dir=build_dir)
@stevester94
Copy link

How should this actually be invoked? IE how does this fit into an existing setup.py?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment