Skip to content

Commit 12d87e5

Browse files
authored
Merge pull request #68 from elezar/update-makefile
Update Makefile to align with other projects.
2 parents d44dcdc + 956cba7 commit 12d87e5

File tree

4 files changed

+177
-92
lines changed

4 files changed

+177
-92
lines changed

.github/workflows/pre-sanity.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Run pre sanity
2+
3+
# run this workflow for each commit
4+
on: [pull_request]
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
12+
- name: Build dev image
13+
run: make .build-image
14+
15+
- name: Build
16+
run: make docker-build
17+
18+
- name: Tests
19+
run: make docker-coverage
20+
21+
- name: Checks
22+
run: make docker-check

Makefile

Lines changed: 128 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,153 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
DOCKER ?= docker
16-
GOLANG_VERSION ?= 1.15
17-
C_FOR_GO_TAG ?= 8eeee8c3b71f9c3c90c4a73db54ed08b0bba971d
18-
BUILDIMAGE ?= nvidia/go-nvml-devel:$(GOLANG_VERSION)-$(C_FOR_GO_TAG)
19-
2015
PWD := $(shell pwd)
2116
GEN_DIR := $(PWD)/gen
2217
PKG_DIR := $(PWD)/pkg
2318
GEN_BINDINGS_DIR := $(GEN_DIR)/nvml
2419
PKG_BINDINGS_DIR := $(PKG_DIR)/nvml
2520

26-
SOURCES = $(shell find $(GEN_BINDINGS_DIR) -type f)
27-
28-
EXAMPLES := $(patsubst ./examples/%/,%,$(sort $(dir $(wildcard ./examples/*/))))
29-
EXAMPLE_TARGETS := $(patsubst %,example-%, $(EXAMPLES))
30-
3121
ifeq ($(shell uname),Darwin)
3222
SED := $(DOCKER) run -it --rm -v "$(PWD):$(PWD)" -w "$(PWD)" alpine:latest sed
3323
else
3424
SED := sed
3525
endif
3626

37-
TARGETS := all test clean bindings test-bindings clean-bindings patch-nvml-h examples $(EXAMPLE_TARGETS)
38-
DOCKER_TARGETS := $(patsubst %, docker-%, $(TARGETS))
39-
.PHONY: $(TARGETS) $(DOCKER_TARGETS)
27+
MODULE := github.com/NVIDIA/go-nvml/pkg
4028

41-
.DEFAULT_GOAL = all
29+
DOCKER ?= docker
4230

43-
all: bindings
44-
test: test-bindings
45-
clean: clean-bindings
31+
GOLANG_VERSION ?= 1.18.10
32+
C_FOR_GO_TAG ?= 8eeee8c3b71f9c3c90c4a73db54ed08b0bba971d
33+
34+
ifeq ($(IMAGE),)
35+
REGISTRY ?= nvidia
36+
IMAGE=$(REGISTRY)/go-nvml
37+
endif
38+
IMAGE_TAG ?= $(GOLANG_VERSION)-$(C_FOR_GO_TAG)
39+
BUILDIMAGE ?= $(IMAGE):$(IMAGE_TAG)-devel
40+
41+
EXAMPLES := $(patsubst ./examples/%/,%,$(sort $(dir $(wildcard ./examples/*/))))
42+
EXAMPLE_TARGETS := $(patsubst %,example-%, $(EXAMPLES))
43+
44+
CMDS := $(patsubst ./cmd/%/,%,$(sort $(dir $(wildcard ./cmd/*/))))
45+
CMD_TARGETS := $(patsubst %,cmd-%, $(CMDS))
46+
47+
CHECK_TARGETS := assert-fmt vet lint
48+
49+
MAKE_TARGETS := binary build all fmt generate test coverage check examples
50+
51+
GENERATE_TARGETS := clean bindings test-bindings clean-bindings patch-nvml-h
52+
53+
TARGETS := $(MAKE_TARGETS) $(EXAMPLE_TARGETS) $(CMD_TARGETS) $(GENERATE_TARGETS) $(CHECK_TARGETS)
54+
55+
DOCKER_TARGETS := $(patsubst %,docker-%, $(TARGETS))
56+
.PHONY: $(TARGETS) $(DOCKER_TARGETS)
4657

4758
GOOS := linux
4859

60+
build:
61+
GOOS=$(GOOS) go build $(MODULE)/...
62+
4963
examples: $(EXAMPLE_TARGETS)
50-
$(EXAMPLE_TARGETS): example-%: bindings
64+
$(EXAMPLE_TARGETS): example-%:
5165
GOOS=$(GOOS) go build ./examples/$(*)
5266

67+
check: $(CHECK_TARGETS)
68+
69+
# Apply go fmt to the codebase
70+
fmt:
71+
go list -f '{{.Dir}}' $(MODULE)/... \
72+
| xargs gofmt -s -l -w
73+
74+
assert-fmt:
75+
go list -f '{{.Dir}}' $(MODULE)/... \
76+
| xargs gofmt -s -l > fmt.out
77+
@if [ -s fmt.out ]; then \
78+
echo "\nERROR: The following files are not formatted:\n"; \
79+
cat fmt.out; \
80+
rm fmt.out; \
81+
exit 1; \
82+
else \
83+
rm fmt.out; \
84+
fi
85+
86+
generate:
87+
go generate $(MODULE)/...
88+
89+
lint:
90+
# We use `go list -f '{{.Dir}}' $(MODULE)/...` to skip the `vendor` folder.
91+
# One we have fixed the linting issues, we whould add -set_exit_status
92+
go list -f '{{.Dir}}' $(MODULE)/... | grep -v pkg/nvml | xargs golint
93+
94+
vet:
95+
go vet $(MODULE)/...
96+
97+
COVERAGE_FILE := coverage.out
98+
test: build
99+
# go test -v -coverprofile=$(COVERAGE_FILE) $(MODULE)/...
100+
@echo "TODO: Skipping tests for now"
101+
102+
coverage: test
103+
# cat $(COVERAGE_FILE) | grep -v "_mock.go" > $(COVERAGE_FILE).no-mocks
104+
# go tool cover -func=$(COVERAGE_FILE).no-mocks
105+
@echo "TODO: Skipping coverage for now"
106+
107+
# Generate an image for containerized builds
108+
# Note: This image is local only
109+
.PHONY: .build-image .pull-build-image .push-build-image
110+
.build-image: docker/Dockerfile.devel
111+
if [ "$(SKIP_IMAGE_BUILD)" = "" ]; then \
112+
$(DOCKER) build \
113+
--progress=plain \
114+
--build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \
115+
--build-arg C_FOR_GO_TAG="$(C_FOR_GO_TAG)" \
116+
--tag $(BUILDIMAGE) \
117+
-f $(^) \
118+
docker; \
119+
fi
120+
121+
.pull-build-image:
122+
$(DOCKER) pull $(BUILDIMAGE)
123+
124+
.push-build-image:
125+
$(DOCKER) push $(BUILDIMAGE)
126+
127+
# A target for executing make targets in a docker container
128+
$(DOCKER_TARGETS): docker-%: .build-image
129+
@echo "Running 'make $(*)' in docker container $(BUILDIMAGE)"
130+
$(DOCKER) run \
131+
--rm \
132+
-e GOCACHE=/tmp/.cache \
133+
-v $(PWD):$(PWD) \
134+
-w $(PWD) \
135+
--user $$(id -u):$$(id -g) \
136+
$(BUILDIMAGE) \
137+
make $(*)
138+
139+
# Start an interactive shell using the development image.
140+
PHONY: .shell
141+
.shell:
142+
$(DOCKER) run \
143+
--rm \
144+
-ti \
145+
-e GOCACHE=/tmp/.cache \
146+
-v $(PWD):$(PWD) \
147+
-w $(PWD) \
148+
--user $$(id -u):$$(id -g) \
149+
$(BUILDIMAGE)
150+
151+
152+
SOURCES = $(shell find $(GEN_BINDINGS_DIR) -type f)
153+
154+
.DEFAULT_GOAL = bindings
155+
156+
# In order to build the packages we need to patch the nvml.h file
157+
build: bindings
158+
159+
test: test-bindings
160+
clean: clean-bindings
161+
53162
$(PKG_BINDINGS_DIR):
54163
mkdir -p $(@)
55164

@@ -60,7 +169,6 @@ $(PKG_BINDINGS_DIR)/nvml.h: $(GEN_BINDINGS_DIR)/nvml.h | $(PKG_BINDINGS_DIR)
60169
spatch --in-place --very-quiet --sp-file $(GEN_BINDINGS_DIR)/anonymous_structs.cocci $(@) > /dev/null
61170

62171
bindings: .create-bindings .strip-autogen-comment .strip-nvml-h-linenumber
63-
64172
.create-bindings: $(PKG_BINDINGS_DIR)/nvml.h $(SOURCES) | $(PKG_BINDINGS_DIR)
65173
cp $(GEN_BINDINGS_DIR)/nvml.yml $(PKG_BINDINGS_DIR)
66174
c-for-go -out $(PKG_DIR) $(PKG_BINDINGS_DIR)/nvml.yml
@@ -84,10 +192,6 @@ bindings: .create-bindings .strip-autogen-comment .strip-nvml-h-linenumber
84192
| xargs $(SED) -i -E 's#$(SED_SEARCH_STRING)$$#$(SED_REPLACE_STRING)#g'
85193

86194
test-bindings: bindings
87-
cd $(PKG_BINDINGS_DIR); \
88-
go test -v .; \
89-
cd -> /dev/null
90-
91195
clean-bindings:
92196
rm -rf $(PKG_BINDINGS_DIR)
93197
git checkout $(PKG_BINDINGS_DIR)
@@ -140,58 +244,9 @@ update-nvml-h:
140244
done; \
141245
echo
142246

143-
# Generate an image for containerized builds
144-
# Note: This image is local only
145-
.build-image: Dockerfile
146-
$(DOCKER) buildx build \
147-
--progress=plain \
148-
--build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \
149-
--build-arg C_FOR_GO_TAG="$(C_FOR_GO_TAG)" \
150-
--tag $(BUILDIMAGE) \
151-
.
152-
153-
# A target for executing make targets in a docker container
154-
$(DOCKER_TARGETS): docker-%: .build-image
155-
@echo "Running 'make $(*)' in docker container $(BUILDIMAGE)"
156-
$(DOCKER) run \
157-
--rm \
158-
-e JQ=jq \
159-
-e GOCACHE=/tmp/.cache \
160-
-v $(PWD):$(PWD) \
161-
-w $(PWD) \
162-
--user $$(id -u):$$(id -g) \
163-
$(BUILDIMAGE) \
164-
make $(*)
165-
166-
# A make target to set up an interactive docker environment as the current user.
167-
# This is useful for debugging issues in the make process in the container.
168-
.docker-shell: .build-image
169-
$(DOCKER) run \
170-
-ti \
171-
--rm \
172-
-e JQ=jq \
173-
-e GOCACHE=/tmp/.cache \
174-
-v $(PWD):$(PWD) \
175-
-w $(PWD) \
176-
--user $$(id -u):$$(id -g) \
177-
$(BUILDIMAGE) \
178-
bash
179-
180-
# A make target to set up an interactive docker environment as root.
181-
# This is useful for debugging issues in dependencies or the container build process
182-
.docker-root-shell: .build-image
183-
$(DOCKER) run \
184-
-ti \
185-
--rm \
186-
-e JQ=jq \
187-
-e GOCACHE=/tmp/.cache \
188-
-v $(PWD):$(PWD) \
189-
-w $(PWD) \
190-
$(BUILDIMAGE) \
191-
bash
192-
193247
# Run markdownlint (https://github.com/markdownlint/markdownlint) for README.md
194248
# Note: Tabs are preferred for Golang code blocks
195249
markdownlint: MDL := $(DOCKER) run --rm -v "$(PWD):$(PWD)" -w "$(PWD)" markdownlint/markdownlint:latest
196250
markdownlint:
197251
@$(MDL) --rules=~no-hard-tabs,~line-length README.md
252+

Dockerfile renamed to docker/Dockerfile.devel

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
FROM ubuntu:22.04
2-
31
ARG TARGETARCH
42
ARG GOLANG_VERSION=0.0.0
3+
FROM ubuntu:22.04
54

65
# Use bash instead of sh
76
SHELL ["/bin/bash", "-c"]
@@ -21,6 +20,16 @@ RUN apt-get update && apt-get install -y \
2120
&& rm -rf /var/lib/apt/lists/* \
2221
&& update-alternatives --install /usr/bin/python python /usr/bin/python3 1
2322

23+
# Install the spatch tool for semantic patching of C code
24+
RUN apt-get update && apt-get install -y \
25+
coccinelle \
26+
&& rm -rf /var/lib/apt/lists/*
27+
28+
29+
ARG TARGETARCH=amd64
30+
ARG GOLANG_VERSION=0.0.0
31+
ENV GOLANG_VERSION=${GOLANG_VERSION}
32+
2433
# Install golang
2534
ENV ARCH=${TARGETARCH}
2635
RUN ARCH=${ARCH/x86_64/amd64} && ARCH=${ARCH/aarch64/arm64} && \
@@ -31,14 +40,13 @@ ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
3140

3241
# Get the supported version of c-for-go. Here we force the use of `GO111MODULE` for go get
3342
# to support the @VERSION syntax.
34-
ARG C_FOR_GO_TAG=master
35-
RUN GO111MODULE=on go get github.com/xlab/c-for-go@${C_FOR_GO_TAG}
43+
ARG C_FOR_GO_TAG=main
44+
RUN go install github.com/xlab/c-for-go@${C_FOR_GO_TAG}
45+
RUN go install golang.org/x/lint/golint@latest
3646

3747
# Set the permissions on the go module path to ensure that this is accessible from
3848
# our user containers.
3949
RUN chmod -R a+rx /go/pkg/mod
4050

41-
# Install the spatch tool for semantic patching of C code
42-
RUN apt-get update && apt-get install -y \
43-
coccinelle \
44-
&& rm -rf /var/lib/apt/lists/*
51+
ENV JQ=jq
52+

pkg/dl/dl.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@ import (
2525
import "C"
2626

2727
const (
28-
RTLD_LAZY = C.RTLD_LAZY
29-
RTLD_NOW = C.RTLD_NOW
30-
RTLD_GLOBAL = C.RTLD_GLOBAL
31-
RTLD_LOCAL = C.RTLD_LOCAL
28+
RTLD_LAZY = C.RTLD_LAZY
29+
RTLD_NOW = C.RTLD_NOW
30+
RTLD_GLOBAL = C.RTLD_GLOBAL
31+
RTLD_LOCAL = C.RTLD_LOCAL
3232
RTLD_NODELETE = C.RTLD_NODELETE
33-
RTLD_NOLOAD = C.RTLD_NOLOAD
33+
RTLD_NOLOAD = C.RTLD_NOLOAD
3434
RTLD_DEEPBIND = C.RTLD_DEEPBIND
3535
)
3636

37-
type DynamicLibrary struct{
38-
Name string
39-
Flags int
37+
type DynamicLibrary struct {
38+
Name string
39+
Flags int
4040
handle unsafe.Pointer
4141
}
4242

4343
func New(name string, flags int) *DynamicLibrary {
4444
return &DynamicLibrary{
45-
Name: name,
46-
Flags: flags,
45+
Name: name,
46+
Flags: flags,
4747
handle: nil,
48-
}
48+
}
4949
}
5050

5151
func (dl *DynamicLibrary) Open() error {

0 commit comments

Comments
 (0)