Skip to content

Commit

Permalink
Fix headers and include paths for NumPy 2 (currently breaks NumPy 1 b…
Browse files Browse the repository at this point in the history
…uild support)
  • Loading branch information
lgarrison committed Jul 26, 2024
1 parent 2cea9a3 commit c562b9e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,14 @@ ifeq ($(DO_CHECKS), 1)
# NUMPY is available -> next step should not fail
# That's why we are not checking if the NUMPY_INCL_FLAG is defined.
ifeq ($(NUMPY_CHECKED), 0)
export NUMPY_INCL_FLAG := $(shell $(PYTHON) -c "from __future__ import print_function; import numpy; print('-isystem ' + numpy.__path__[0] + '/core/include/numpy/')")
export NUMPY_INCL_FLAG := $(shell $(PYTHON) -c "from __future__ import print_function; import numpy; print('-isystem ' + numpy.__path__[0] + '/_core/include/')")
# Take the second word -> the path (the first word is "isystem")
NUMPY_INCL_PATH := $(word 2, ${NUMPY_INCL_FLAG})
# Now check that the 'arrayobject.h' file is present in the
# supposed numpy directory. Otherwise, compilation will fail.
# The absence of the file likely indicates a missing numpy-devel
# package (see issue #134 on github)
NUMPY_NEEDED_HEADER_FILE := ${NUMPY_INCL_PATH}arrayobject.h
NUMPY_NEEDED_HEADER_FILE := ${NUMPY_INCL_PATH}numpy/arrayobject.h
ifeq (,$(wildcard ${NUMPY_NEEDED_HEADER_FILE}))
$(error Required $(ccred)numpy headers$(ccreset) are missing...stopping the compilation. You might be able to fix this by installing $(ccblue)numpy-devel$(ccreset))
endif
Expand Down
2 changes: 1 addition & 1 deletion mocks/python_bindings/_countpairs_mocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <Python.h>

/* Now, include the numpy header*/
#include <arrayobject.h>
#include <numpy/arrayobject.h>

//for correlation functions
#include "countpairs_rp_pi_mocks.h"
Expand Down
2 changes: 1 addition & 1 deletion theory/python_bindings/_countpairs.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <stdint.h>

/* Now, include the numpy header*/
#include <arrayobject.h>
#include <numpy/arrayobject.h>

//for correlation functions
#include "countpairs.h"
Expand Down

2 comments on commit c562b9e

@manodeep
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgarrison Maybe the best solution is to keep the include statements within the C codes as is and change the NUMPY_INCL flag based on the numpy version.

So something like this (completely untested):

IS_NUMPY_2  := $(shell [ $(NUMPY_VERSION_MAJOR) -gt 2 ] && echo true)
ifeq ($(IS_NUMPY_2),true)
  export NUMPY_INCL_FLAG := $(shell $(PYTHON) -c "from __future__ import print_function; import numpy; print('-isystem ' + numpy.__path__[0] + '/_core/include/')")
  export NUMPY_NEEDED_HEADER_FILE := ${NUMPY_INCL_PATH}numpy/arrayobject.h
else
  export NUMPY_INCL_FLAG := $(shell $(PYTHON) -c "from __future__ import print_function; import numpy; print('-isystem ' + numpy.__path__[0] + '/_core/include/numpy/')")
  export NUMPY_NEEDED_HEADER_FILE := ${NUMPY_INCL_PATH}arrayobject.h
endif

@lgarrison
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work because the headers are in include/numpy/, not include/. But I think we can just use numpy.get_include() and it will give the right path for each NumPy. I'll open a PR.

Please sign in to comment.