@@ -59,13 +59,50 @@ BUILD_REF ?= $(shell git describe --abbrev=0 2>/dev/null)
5959# go source files
6060SRC = $(shell find . -type f -name '* .go')
6161
62+ LDFLAGS ?= -extldflags '-L$(shell pwd) /lib'
63+
64+ # #@ Python Configuration
65+
66+ PYTHON_VERSION := 3.12
67+
68+ # Unified Python configuration detection. This block runs once.
69+ # It prioritizes python-config, then pkg-config, for reliability.
70+ ifeq ($(TARGETOS ) ,darwin)
71+ # macOS: Find Homebrew's python-config script for the most reliable flags.
72+ BREW_PREFIX := $(shell command -v brew >/dev/null 2>&1 && brew --prefix python@$(PYTHON_VERSION ) 2>/dev/null)
73+ PYTHON_CONFIG := $(BREW_PREFIX ) /bin/python$(PYTHON_VERSION ) -config
74+ PYTHON_ERROR := "Could not execute 'python$(PYTHON_VERSION ) -config' from Homebrew. Please ensure Python is installed correctly with: 'brew install python@$(PYTHON_VERSION ) '"
75+ else ifeq ($(TARGETOS),linux)
76+ # Linux: Use standard system tools to find flags.
77+ PYTHON_CONFIG := $(shell command -v python$(PYTHON_VERSION ) -config || command -v python3-config)
78+ PYTHON_ERROR := "Python $(PYTHON_VERSION ) development headers not found. Please install with: 'sudo apt install python$(PYTHON_VERSION ) -dev' or 'sudo dnf install python$(PYTHON_VERSION ) -devel'"
79+ else
80+ $(error "Unsupported OS : $(TARGETOS ) ")
81+ endif
82+
83+ ifneq ($(shell $(PYTHON_CONFIG ) --cflags 2>/dev/null) ,)
84+ PYTHON_CFLAGS := $(shell $(PYTHON_CONFIG ) --cflags)
85+ # Use --ldflags --embed to get all necessary flags for linking
86+ PYTHON_LDFLAGS := $(shell $(PYTHON_CONFIG ) --ldflags --embed)
87+ else
88+ $(error ${PYTHON_ERROR})
89+ endif
90+
91+ # CGO flags with all dependencies
92+ CGO_CFLAGS := $(PYTHON_CFLAGS ) '-I$(shell pwd) /lib'
93+ CGO_LDFLAGS := $(PYTHON_LDFLAGS ) $(PYTHON_LIBS ) '-L$(shell pwd) /lib' -ltokenizers -ldl -lm
94+
6295# Internal variables for generic targets
6396epp_IMAGE = $(EPP_IMAGE )
6497sidecar_IMAGE = $(SIDECAR_IMAGE )
6598epp_NAME = epp
6699sidecar_NAME = $(SIDECAR_NAME )
67100epp_LDFLAGS = -ldflags="$(LDFLAGS ) "
68101sidecar_LDFLAGS =
102+ epp_CGO_CFLAGS = "${CGO_CFLAGS}"
103+ sidecar_CGO_CFLAGS =
104+ epp_CGO_LDFLAGS = "${CGO_LDFLAGS}"
105+ sidecar_CGO_LDFLAGS =
69106epp_TEST_FILES = go list ./... | grep -v /test/ | grep -v ./pkg/sidecar/
70107sidecar_TEST_FILES = go list ./pkg/sidecar/...
71108
@@ -75,7 +112,6 @@ help: ## Print help
75112
76113# #@ Tokenizer & Linking
77114
78- LDFLAGS ?= -extldflags '-L$(shell pwd) /lib'
79115CGO_ENABLED =1
80116TOKENIZER_LIB = lib/libtokenizers.a
81117# Extract RELEASE_VERSION from Dockerfile
@@ -112,7 +148,7 @@ test-unit: test-unit-epp test-unit-sidecar
112148.PHONY : test-unit-%
113149test-unit-% : download-tokenizer install-dependencies # # Run unit tests
114150 @printf " \033[33;1m==== Running Unit Tests ====\033[0m\n"
115- go test $($* _LDFLAGS) -v $$($($* _TEST_FILES) | tr '\n' ' ')
151+ CGO_CFLAGS= ${ $* _CGO_CFLAGS} CGO_LDFLAGS= ${ $* _CGO_LDFLAGS} go test $($* _LDFLAGS) -v $$($($* _TEST_FILES) | tr '\n' ' ')
116152
117153.PHONY : test-integration
118154test-integration : download-tokenizer install-dependencies # # Run integration tests
@@ -132,7 +168,7 @@ post-deploy-test: ## Run post deployment tests
132168.PHONY : lint
133169lint : check-golangci-lint check-typos # # Run lint
134170 @printf " \033[33;1m==== Running linting ====\033[0m\n"
135- golangci-lint run
171+ CGO_CFLAGS= " ${CGO_CFLAGS} " golangci-lint run
136172 $(TYPOS )
137173
138174# #@ Build
0 commit comments