From ac68e1e6f1671a18e22690020b5309698adcbe4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ibrahim=20Numanagi=C4=87?=
 <inumanag@users.noreply.github.com>
Date: Sun, 18 Dec 2022 10:12:32 -0800
Subject: [PATCH] Fix ABI incompatibilities (#119)

* Fix ABI incompatibilities

* Fix codon-jit on macOS
---
 CMakeLists.txt                                |  3 ++-
 cmake/config.py.in                            |  3 +--
 codon/compiler/jit.h                          |  2 +-
 .../jit.h => codon/compiler/jit_extern.h      |  0
 extra/python/MANIFEST.in                      |  1 -
 extra/python/codon/jit.pxd                    |  2 +-
 extra/python/setup.py                         | 24 +++++++++++++++----
 7 files changed, 24 insertions(+), 11 deletions(-)
 rename extra/python/codon/jit.h => codon/compiler/jit_extern.h (100%)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f59f39e0..fef4b21c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,6 +4,7 @@ project(
   VERSION "0.15.2"
   HOMEPAGE_URL "https://github.com/exaloop/codon"
   DESCRIPTION "high-performance, extensible Python compiler")
+set(CODON_JIT_PYTHON_VERSION "0.1.1")
 configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in"
                "${PROJECT_SOURCE_DIR}/codon/config/config.h")
 configure_file("${PROJECT_SOURCE_DIR}/cmake/config.py.in"
@@ -216,7 +217,7 @@ set(CODON_HPPFILES
     codon/sir/var.h
     codon/util/common.h
     extra/jupyter/jupyter.h
-    extra/python/codon/jit.h)
+    codon/compiler/jit_extern.h)
 set(CODON_CPPFILES
     codon/compiler/compiler.cpp
     codon/compiler/debug_listener.cpp
diff --git a/cmake/config.py.in b/cmake/config.py.in
index f5f02476..11a88f6f 100644
--- a/cmake/config.py.in
+++ b/cmake/config.py.in
@@ -1,5 +1,4 @@
-__version__         = "0.1"
-
+__version__         = "@CODON_JIT_PYTHON_VERSION@"
 CODON_VERSION       = "@PROJECT_VERSION@"
 CODON_VERSION_MAJOR = @PROJECT_VERSION_MAJOR@
 CODON_VERSION_MINOR = @PROJECT_VERSION_MINOR@
diff --git a/codon/compiler/jit.h b/codon/compiler/jit.h
index 687d3eed..1c8e0060 100644
--- a/codon/compiler/jit.h
+++ b/codon/compiler/jit.h
@@ -16,7 +16,7 @@
 #include "codon/sir/transform/manager.h"
 #include "codon/sir/var.h"
 
-#include "extra/python/codon/jit.h"
+#include "codon/compiler/jit_extern.h"
 
 namespace codon {
 namespace jit {
diff --git a/extra/python/codon/jit.h b/codon/compiler/jit_extern.h
similarity index 100%
rename from extra/python/codon/jit.h
rename to codon/compiler/jit_extern.h
diff --git a/extra/python/MANIFEST.in b/extra/python/MANIFEST.in
index bd0789c0..e69de29b 100644
--- a/extra/python/MANIFEST.in
+++ b/extra/python/MANIFEST.in
@@ -1 +0,0 @@
-include codon/jit.h
diff --git a/extra/python/codon/jit.pxd b/extra/python/codon/jit.pxd
index fcb2ded7..e6ad45b9 100644
--- a/extra/python/codon/jit.pxd
+++ b/extra/python/codon/jit.pxd
@@ -3,7 +3,7 @@
 from libcpp.string cimport string
 from libcpp.vector cimport vector
 
-cdef extern from "jit.h" namespace "codon::jit":
+cdef extern from "codon/compiler/jit_extern.h" namespace "codon::jit":
     cdef cppclass JIT
     cdef cppclass JITResult:
         void *result
diff --git a/extra/python/setup.py b/extra/python/setup.py
index 7ea648f5..f59ae821 100644
--- a/extra/python/setup.py
+++ b/extra/python/setup.py
@@ -3,6 +3,7 @@
 import os
 import sys
 import shutil
+import subprocess
 from pathlib import Path
 from Cython.Distutils import build_ext
 from setuptools import setup
@@ -45,18 +46,31 @@ print("Codon: " + str(codon_path))
 
 
 if sys.platform == "darwin":
-    linker_args = "-Wl,-rpath," + str(codon_path / "lib" / "codon")
+    libraries=["codonrt", "codonc"]
+    linker_args = ["-Wl,-rpath," + str(codon_path / "lib" / "codon")]
 else:
-    linker_args = "-Wl,-rpath=" + str(codon_path / "lib" / "codon")
+    libraries=["codonrt"]
+    linker_args = [
+        "-Wl,-rpath=" + str(codon_path / "lib" / "codon"),
+        "-Wl,--no-as-needed",
+        "-lcodonc",
+    ]
 
+    # TODO: handle ABI changes better
+    out = subprocess.check_output(["nm", "-g", str(codon_path / "lib" / "codon" / "libcodonc.so")])
+    out = [i for i in out.decode(sys.stdout.encoding).split("\n") if "jitExecuteSafe" in i]
+    if out and "cxx11" not in out[0]:
+        print("CXX11 ABI not detected")
+        os.environ["CFLAGS"] = os.environ.get("CFLAGS", "") + " -D_GLIBCXX_USE_CXX11_ABI=0"
 
 jit_extension = Extension(
     "codon.codon_jit",
     sources=["codon/jit.pyx", "codon/jit.pxd"],
-    libraries=["codonc", "codonrt"],
+    libraries=libraries,
     language="c++",
-    extra_compile_args=["-w", "-std=c++17"],
-    extra_link_args=[linker_args],
+    extra_compile_args=["-w"],
+    extra_link_args=linker_args,
+    include_dirs=[str(codon_path / "include")],
     library_dirs=[str(codon_path / "lib" / "codon")],
 )