From fe2f083833f53168315aace536f095a14bf030e2 Mon Sep 17 00:00:00 2001 From: murilobalves Date: Mon, 27 Jul 2026 14:36:42 +0200 Subject: [PATCH] ENH: Adapt code to also compile on MacOS --- Makefile | 5 ++++- include/trackcpp/auxiliary.h | 2 +- python_package/Makefile | 18 +++++++++++++++--- src/output.cpp | 8 ++++---- src/trackcpp.cpp | 2 ++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index d573057..31c98b0 100644 --- a/Makefile +++ b/Makefile @@ -117,6 +117,9 @@ CFLAGS += $(WARNINGS_CFLAGS) LIBOBJECTS = $(addprefix $(OBJDIR)/, $(LIBSOURCES_CPP:.cpp=.o)) BINOBJECTS = $(addprefix $(OBJDIR)/, $(BINSOURCES_CPP:.cpp=.o)) LDFLAGS = $(MACHINE) +ifneq ($(CONDA_PREFIX),) + LDFLAGS += -Wl,-rpath,$(CONDA_PREFIX)/lib +endif #### DERIVED CONDITIONALS AND VARIABLES #### ifeq ($(shell hostname), uv100) @@ -134,7 +137,7 @@ endif all: libtrackcpp trackcpp python_package #### GENERATES DEPENDENCY FILE #### -$(shell $(CXX) -MM $(CFLAGS) $(addprefix $(SRCDIR)/, $(LIBSOURCES_CPP)) $(addprefix $(SRCDIR)/, $(BINSOURCES_CPP)) | sed 's/.*\.o/$(OBJDIR)\/&/' > .depend) +$(shell $(CXX) -MM $(CFLAGS) $(INC) $(addprefix $(SRCDIR)/, $(LIBSOURCES_CPP)) $(addprefix $(SRCDIR)/, $(BINSOURCES_CPP)) | sed 's/.*\.o/$(OBJDIR)\/&/' > .depend) -include .depend libtrackcpp: $(OBJDIR)/libtrackcpp.a diff --git a/include/trackcpp/auxiliary.h b/include/trackcpp/auxiliary.h index 35e2956..c3bdf13 100644 --- a/include/trackcpp/auxiliary.h +++ b/include/trackcpp/auxiliary.h @@ -218,7 +218,7 @@ void set_random_distribution(unsigned value); double gen_random_number(); -#if __GNUC__ < 6 +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 6 bool isfinite(const double& v); #endif diff --git a/python_package/Makefile b/python_package/Makefile index 06edce0..7f1b110 100644 --- a/python_package/Makefile +++ b/python_package/Makefile @@ -34,19 +34,31 @@ INC += -I../include $(PYTHON_INCLUDES) $(NUMPY_INCLUDE) INTERFACESRCS = $(INTERFACEOBJS:.o=.cpp) INTERFACEHDRS = $(INTERFACEOBJS:.o=.h) +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Darwin) + SOFLAGS = -bundle -undefined dynamic_lookup + ifneq ($(CONDA_PREFIX),) + SOFLAGS += -Wl,-rpath,$(CONDA_PREFIX)/lib + endif + ARCHIVE_LINK = -Wl,-force_load,$(TRACKCPPLIB) +else + SOFLAGS = -shared + ARCHIVE_LINK = -Wl,--whole-archive $(TRACKCPPLIB) -Wl,--no-whole-archive +endif + .NOTPARALLEL: all: trackcpp/_trackcpp.so -$(shell $(CXX) -MM $(INTERFACESRCS) > .depend) +$(shell $(CXX) -MM $(INC) $(INTERFACESRCS) > .depend) -include .depend trackcpp/_trackcpp.so: $(TRACKCPPLIB) $(OBJECTS) - $(CXX) -shared -Wl,--whole-archive $(TRACKCPPLIB) -Wl,--no-whole-archive $(OBJECTS) $(LIBS) -o trackcpp/_trackcpp.so + $(CXX) $(SOFLAGS) $(ARCHIVE_LINK) $(OBJECTS) $(LIBS) -o trackcpp/_trackcpp.so trackcpp_wrap.cxx: $(SOURCES_I) $(INTERFACESRCS) $(INTERFACEHDRS) [ -f ./numpy.i ] && echo "numpy.i already here, good" || \ - wget https://raw.githubusercontent.com/numpy/numpy/v$(NUMPY_VERSION)/tools/swig/numpy.i + (command -v wget >/dev/null 2>&1 && wget https://raw.githubusercontent.com/numpy/numpy/v$(NUMPY_VERSION)/tools/swig/numpy.i || curl -sL -o numpy.i https://raw.githubusercontent.com/numpy/numpy/v$(NUMPY_VERSION)/tools/swig/numpy.i) $(SWIG) -c++ -python $(INC) trackcpp.i && cp -f trackcpp.py trackcpp install: uninstall all diff --git a/src/output.cpp b/src/output.cpp index 1a83ca1..25a34e0 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -40,7 +40,7 @@ Status::type print_closed_orbit(const Accelerator& accelerator, const std::vecto fprintf(fp, "# ebeam_energy[eV] : %f\n", accelerator.energy); fprintf(fp, "# harmonic_number : %i\n", accelerator.harmonic_number); fprintf(fp, "# cavity_state : %s\n", accelerator.cavity_on ? "on" : "off"); - fprintf(fp, "# radiation_state : %s\n", rad_dict[accelerator.radiation_on]); + fprintf(fp, "# radiation_state : %s\n", rad_dict[accelerator.radiation_on].c_str()); fprintf(fp, "# chamber_state : %s\n", accelerator.vchamber_on ? "on" : "off"); fprintf(fp, "\n"); fprintf(fp, "%-5s %-15s %-24s %-24s %-24s %-24s %-24s %-24s %-24s\n", "# idx", "fam_name", "s[m]", "rx[m]", "px[rad]", "ry[m]", "py[rad]", "de", "dl[m]"); @@ -70,7 +70,7 @@ Status::type print_tracking_linepass(const Accelerator& accelerator, const std:: fprintf(fp, "# ebeam_energy[eV] : %f\n", accelerator.energy); fprintf(fp, "# harmonic_number : %i\n", accelerator.harmonic_number); fprintf(fp, "# cavity_state : %s\n", accelerator.cavity_on ? "on" : "off"); - fprintf(fp, "# radiation_state : %s\n", rad_dict[accelerator.radiation_on]); + fprintf(fp, "# radiation_state : %s\n", rad_dict[accelerator.radiation_on].c_str()); fprintf(fp, "# chamber_state : %s\n", accelerator.vchamber_on ? "on" : "off"); fprintf(fp, "\n"); @@ -104,7 +104,7 @@ Status::type print_tracking_ringpass(const Accelerator& accelerator, const std:: fprintf(fp, "# ebeam_energy[eV] : %f\n", accelerator.energy); fprintf(fp, "# harmonic_number : %i\n", accelerator.harmonic_number); fprintf(fp, "# cavity_state : %s\n", accelerator.cavity_on ? "on" : "off"); - fprintf(fp, "# radiation_state : %s\n", rad_dict[accelerator.radiation_on]); + fprintf(fp, "# radiation_state : %s\n", rad_dict[accelerator.radiation_on].c_str()); fprintf(fp, "# chamber_state : %s\n", accelerator.vchamber_on ? "on" : "off"); fprintf(fp, "\n"); @@ -135,7 +135,7 @@ Status::type print_dynapgrid(const Accelerator& accelerator, const std::vector