Skip to content

Commit

Permalink
Merge pull request #26 from spoorcc/issue-10
Browse files Browse the repository at this point in the history
Fix #10: Added magics to provide LDFLAGS and CFLAGS to gcc
  • Loading branch information
brendan-rius committed Apr 12, 2017
2 parents 608c737 + 36c1f7f commit 17316fb
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions jupyter_c_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,36 @@ def create_jupyter_subprocess(self, cmd):
lambda contents: self._write_to_stdout(contents.decode()),
lambda contents: self._write_to_stderr(contents.decode()))

def compile_with_gcc(self, source_filename, binary_filename):
args = ['gcc', source_filename, '-std=c11', '-fPIC', '-shared', '-rdynamic', '-o', binary_filename]
def compile_with_gcc(self, source_filename, binary_filename, cflags=None, ldflags=None):
cflags = ['-std=c11', '-fPIC', '-shared', '-rdynamic'] + cflags
args = ['gcc', source_filename] + cflags + ['-o', binary_filename] + ldflags
return self.create_jupyter_subprocess(args)

def _filter_magics(self, code):

magics = {'cflags': [], 'ldflags': []}

for line in code.splitlines():
if line.startswith('//%'):
key, value = line[3:].split(":", 2)
key = key.strip().lower()

if key in ['ldflags', 'cflags']:
for flag in value.split():
magics[key] += [flag]

return magics

def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):

magics = self._filter_magics(code)

with self.new_temp_file(suffix='.c') as source_file:
source_file.write(code)
source_file.flush()
with self.new_temp_file(suffix='.out') as binary_file:
p = self.compile_with_gcc(source_file.name, binary_file.name)
p = self.compile_with_gcc(source_file.name, binary_file.name, magics['cflags'], magics['ldflags'])
while p.poll() is None:
p.write_contents()
p.write_contents()
Expand Down

0 comments on commit 17316fb

Please sign in to comment.