Skip to content

Commit

Permalink
Improve Makefile (stashapp#3901)
Browse files Browse the repository at this point in the history
* Improve Makefile
* Make ui targets consistent
---------
Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
  • Loading branch information
DingDongSoLong4 authored Jul 12, 2023
1 parent 278a064 commit 96f2229
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
- name: Validate UI
# skip UI validation for pull requests if UI is unchanged
if: ${{ github.event_name != 'pull_request' || steps.cache-ui.outputs.cache-hit != 'true' }}
run: docker exec -t build /bin/bash -c "make validate-frontend"
run: docker exec -t build /bin/bash -c "make validate-ui"

# Static validation happens in the linter workflow in parallel to this workflow
# Run Dynamic validation here, to make sure we pass all the projects integration tests
Expand Down
199 changes: 138 additions & 61 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ endif
LDFLAGS := $(LDFLAGS)

# set OUTPUT environment variable to generate a specific binary name
# this will apply to both `stash` and `phasher`, so build them separately
# alternatively use STASH_OUTPUT or PHASHER_OUTPUT to set the value individually
ifdef OUTPUT
OUTPUT := -o $(OUTPUT)
STASH_OUTPUT := $(OUTPUT)
PHASHER_OUTPUT := $(OUTPUT)
endif
ifdef STASH_OUTPUT
STASH_OUTPUT := -o $(STASH_OUTPUT)
endif
ifdef PHASHER_OUTPUT
PHASHER_OUTPUT := -o $(PHASHER_OUTPUT)
endif

# set GO_BUILD_FLAGS environment variable to any extra build flags required
GO_BUILD_FLAGS := $(GO_BUILD_FLAGS)
GO_BUILD_FLAGS += -buildmode=pie

# set GO_BUILD_TAGS environment variable to any extra build tags required
GO_BUILD_TAGS := $(GO_BUILD_TAGS)
Expand All @@ -41,89 +49,147 @@ export CGO_ENABLED := 1
.PHONY: release
release: pre-ui generate ui build-release

.PHONY: pre-build
pre-build:
# targets to set various build flags

.PHONY: flags-release
flags-release:
$(eval LDFLAGS += -s -w)
$(eval GO_BUILD_FLAGS += -trimpath)

.PHONY: flags-pie
flags-pie:
$(eval GO_BUILD_FLAGS += -buildmode=pie)

.PHONY: flags-static
flags-static:
$(eval LDFLAGS += -extldflags=-static)
$(eval GO_BUILD_TAGS += sqlite_omit_load_extension osusergo netgo)

.PHONY: flags-static-pie
flags-static-pie:
$(eval LDFLAGS += -extldflags=-static-pie)
$(eval GO_BUILD_FLAGS += -buildmode=pie)
$(eval GO_BUILD_TAGS += sqlite_omit_load_extension osusergo netgo)

.PHONY: flags-static-windows
flags-static-windows:
$(eval LDFLAGS += -extldflags=-static-pie)
$(eval GO_BUILD_FLAGS += -buildmode=pie)
$(eval GO_BUILD_TAGS += sqlite_omit_load_extension osusergo)

.PHONY: build-info
build-info:
ifndef BUILD_DATE
$(eval BUILD_DATE := $(shell go run scripts/getDate.go))
endif

ifndef GITHASH
$(eval GITHASH := $(shell git rev-parse --short HEAD))
endif

ifndef STASH_VERSION
$(eval STASH_VERSION := $(shell git describe --tags --exclude latest_develop))
endif

ifndef OFFICIAL_BUILD
$(eval OFFICIAL_BUILD := false)
endif

.PHONY: build-flags
build-flags: pre-build
build-flags: build-info
$(eval BUILD_LDFLAGS := $(LDFLAGS))
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.buildstamp=$(BUILD_DATE)')
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.githash=$(GITHASH)')
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.version=$(STASH_VERSION)')
$(eval BUILD_LDFLAGS += -X 'github.com/stashapp/stash/internal/build.officialBuild=$(OFFICIAL_BUILD)')
$(eval BUILD_FLAGS := -v -tags "$(GO_BUILD_TAGS)" $(GO_BUILD_FLAGS) -ldflags "$(BUILD_LDFLAGS)")

# builds a dynamically-linked debug binary
.PHONY: build
build: build-flags
build:
go build $(OUTPUT) $(BUILD_FLAGS) ./cmd/stash
.PHONY: stash
stash: build-flags
go build $(STASH_OUTPUT) $(BUILD_FLAGS) ./cmd/stash

.PHONY: stash-release
stash-release: flags-release
stash-release: flags-pie
stash-release: stash

.PHONY: stash-release-static
stash-release-static: flags-release
stash-release-static: flags-static-pie
stash-release-static: stash

.PHONY: stash-release-static-windows
stash-release-static-windows: flags-release
stash-release-static-windows: flags-static-windows
stash-release-static-windows: stash

# TODO: Integrate the phasher target with the rest of the Makefile,
# TODO: so it can be built as part of normal releases.
.PHONY: phasher
phasher:
go build -o $@ -trimpath -buildmode=pie -ldflags '-s -w' ./cmd/phasher
phasher: build-flags
go build $(PHASHER_OUTPUT) $(BUILD_FLAGS) ./cmd/phasher

# builds a dynamically-linked release binary
.PHONY: phasher-release
phasher-release: flags-release
phasher-release: flags-pie
phasher-release: phasher

.PHONY: phasher-release-static
phasher-release-static: flags-release
phasher-release-static: flags-static-pie
phasher-release-static: phasher

.PHONY: phasher-release-static-windows
phasher-release-static-windows: flags-release
phasher-release-static-windows: flags-static-windows
phasher-release-static-windows: phasher

# builds dynamically-linked debug binaries
.PHONY: build
build: stash phasher

# builds dynamically-linked release binaries
.PHONY: build-release
build-release: LDFLAGS += -s -w
build-release: GO_BUILD_FLAGS += -trimpath
build-release: build
build-release: stash-release phasher-release

# builds a statically-linked release binary
# builds statically-linked release binaries
.PHONY: build-release-static
build-release-static: GO_BUILD_TAGS += netgo
build-release-static: build-release-static-windows
build-release-static: stash-release-static phasher-release-static

# build-release-static, but excluding netgo, which is not needed on windows
.PHONY: build-release-static-windows
build-release-static-windows: LDFLAGS += -extldflags=-static -s -w
build-release-static-windows: GO_BUILD_FLAGS += -trimpath
build-release-static-windows: GO_BUILD_TAGS += sqlite_omit_load_extension osusergo
build-release-static-windows: build
build-release-static-windows: stash-release-static-windows phasher-release-static-windows

# cross-compile- targets should be run within the compiler docker container
.PHONY: cross-compile-windows
cross-compile-windows: export GOOS := windows
cross-compile-windows: export GOARCH := amd64
cross-compile-windows: export CC := x86_64-w64-mingw32-gcc
cross-compile-windows: export CXX := x86_64-w64-mingw32-g++
cross-compile-windows: OUTPUT := -o dist/stash-win.exe
cross-compile-windows: build-release-static-windows
cross-compile-windows: STASH_OUTPUT := -o dist/stash-win.exe
cross-compile-windows: PHASHER_OUTPUT := -o dist/phasher-win.exe
cross-compile-windows: flags-release
cross-compile-windows: flags-static-windows
cross-compile-windows: build

.PHONY: cross-compile-macos-intel
cross-compile-macos-intel: export GOOS := darwin
cross-compile-macos-intel: export GOARCH := amd64
cross-compile-macos-intel: export CC := o64-clang
cross-compile-macos-intel: export CXX := o64-clang++
cross-compile-macos-intel: OUTPUT := -o dist/stash-macos-intel
cross-compile-macos-intel: STASH_OUTPUT := -o dist/stash-macos-intel
cross-compile-macos-intel: PHASHER_OUTPUT := -o dist/phasher-macos-intel
cross-compile-macos-intel: flags-release
# can't use static build for OSX
cross-compile-macos-intel: build-release
cross-compile-macos-intel: flags-pie
cross-compile-macos-intel: build

.PHONY: cross-compile-macos-applesilicon
cross-compile-macos-applesilicon: export GOOS := darwin
cross-compile-macos-applesilicon: export GOARCH := arm64
cross-compile-macos-applesilicon: export CC := oa64e-clang
cross-compile-macos-applesilicon: export CXX := oa64e-clang++
cross-compile-macos-applesilicon: OUTPUT := -o dist/stash-macos-applesilicon
cross-compile-macos-applesilicon: STASH_OUTPUT := -o dist/stash-macos-applesilicon
cross-compile-macos-applesilicon: PHASHER_OUTPUT := -o dist/phasher-macos-applesilicon
cross-compile-macos-applesilicon: flags-release
# can't use static build for OSX
cross-compile-macos-applesilicon: build-release
cross-compile-macos-applesilicon: flags-pie
cross-compile-macos-applesilicon: build

.PHONY: cross-compile-macos
cross-compile-macos:
Expand All @@ -143,37 +209,52 @@ cross-compile-macos:
.PHONY: cross-compile-freebsd
cross-compile-freebsd: export GOOS := freebsd
cross-compile-freebsd: export GOARCH := amd64
cross-compile-freebsd: OUTPUT := -o dist/stash-freebsd
cross-compile-freebsd: build-release-static
cross-compile-freebsd: STASH_OUTPUT := -o dist/stash-freebsd
cross-compile-freebsd: PHASHER_OUTPUT := -o dist/phasher-freebsd
cross-compile-freebsd: flags-release
cross-compile-freebsd: flags-static-pie
cross-compile-freebsd: build

.PHONY: cross-compile-linux
cross-compile-linux: export GOOS := linux
cross-compile-linux: export GOARCH := amd64
cross-compile-linux: OUTPUT := -o dist/stash-linux
cross-compile-linux: build-release-static
cross-compile-linux: STASH_OUTPUT := -o dist/stash-linux
cross-compile-linux: PHASHER_OUTPUT := -o dist/phasher-linux
cross-compile-linux: flags-release
cross-compile-linux: flags-static-pie
cross-compile-linux: build

.PHONY: cross-compile-linux-arm64v8
cross-compile-linux-arm64v8: export GOOS := linux
cross-compile-linux-arm64v8: export GOARCH := arm64
cross-compile-linux-arm64v8: export CC := aarch64-linux-gnu-gcc
cross-compile-linux-arm64v8: OUTPUT := -o dist/stash-linux-arm64v8
cross-compile-linux-arm64v8: build-release-static
cross-compile-linux-arm64v8: STASH_OUTPUT := -o dist/stash-linux-arm64v8
cross-compile-linux-arm64v8: PHASHER_OUTPUT := -o dist/phasher-linux-arm64v8
cross-compile-linux-arm64v8: flags-release
cross-compile-linux-arm64v8: flags-static-pie
cross-compile-linux-arm64v8: build

.PHONY: cross-compile-linux-arm32v7
cross-compile-linux-arm32v7: export GOOS := linux
cross-compile-linux-arm32v7: export GOARCH := arm
cross-compile-linux-arm32v7: export GOARM := 7
cross-compile-linux-arm32v7: export CC := arm-linux-gnueabihf-gcc
cross-compile-linux-arm32v7: OUTPUT := -o dist/stash-linux-arm32v7
cross-compile-linux-arm32v7: build-release-static
cross-compile-linux-arm32v7: STASH_OUTPUT := -o dist/stash-linux-arm32v7
cross-compile-linux-arm32v7: PHASHER_OUTPUT := -o dist/phasher-linux-arm32v7
cross-compile-linux-arm32v7: flags-release
cross-compile-linux-arm32v7: flags-static
cross-compile-linux-arm32v7: build

.PHONY: cross-compile-linux-arm32v6
cross-compile-linux-arm32v6: export GOOS := linux
cross-compile-linux-arm32v6: export GOARCH := arm
cross-compile-linux-arm32v6: export GOARM := 6
cross-compile-linux-arm32v6: export CC := arm-linux-gnueabi-gcc
cross-compile-linux-arm32v6: OUTPUT := -o dist/stash-linux-arm32v6
cross-compile-linux-arm32v6: build-release-static
cross-compile-linux-arm32v6: STASH_OUTPUT := -o dist/stash-linux-arm32v6
cross-compile-linux-arm32v6: PHASHER_OUTPUT := -o dist/phasher-linux-arm32v6
cross-compile-linux-arm32v6: flags-release
cross-compile-linux-arm32v6: flags-static
cross-compile-linux-arm32v6: build

.PHONY: cross-compile-all
cross-compile-all:
Expand All @@ -197,10 +278,10 @@ endif

# Regenerates GraphQL files
.PHONY: generate
generate: generate-backend generate-frontend
generate: generate-backend generate-ui

.PHONY: generate-frontend
generate-frontend:
.PHONY: generate-ui
generate-ui:
cd ui/v2.5 && yarn run gqlgen

.PHONY: generate-backend
Expand Down Expand Up @@ -264,7 +345,7 @@ pre-ui:
cd ui/v2.5 && yarn install --frozen-lockfile

.PHONY: ui-env
ui-env: pre-build
ui-env: build-info
$(eval export VITE_APP_DATE := $(BUILD_DATE))
$(eval export VITE_APP_GITHASH := $(GITHASH))
$(eval export VITE_APP_STASH_VERSION := $(STASH_VERSION))
Expand Down Expand Up @@ -295,29 +376,25 @@ ui-start: ui-env
fmt-ui:
cd ui/v2.5 && yarn format

# runs tests and checks on the UI and builds it
.PHONY: ui-validate
ui-validate:
cd ui/v2.5 && yarn run validate

# runs all of the tests and checks required for a PR to be accepted
.PHONY: validate
validate: validate-frontend validate-backend

# runs all of the frontend PR-acceptance steps
.PHONY: validate-frontend
validate-frontend: ui-validate
.PHONY: validate-ui
validate-ui:
cd ui/v2.5 && yarn run validate

# runs all of the backend PR-acceptance steps
.PHONY: validate-backend
validate-backend: lint it

# runs all of the tests and checks required for a PR to be accepted
.PHONY: validate
validate: validate-ui validate-backend

# locally builds and tags a 'stash/build' docker image
.PHONY: docker-build
docker-build: pre-build
docker-build: build-info
docker build --build-arg GITHASH=$(GITHASH) --build-arg STASH_VERSION=$(STASH_VERSION) -t stash/build -f docker/build/x86_64/Dockerfile .

# locally builds and tags a 'stash/cuda-build' docker image
.PHONY: docker-cuda-build
docker-cuda-build: pre-build
docker-cuda-build: build-info
docker build --build-arg GITHASH=$(GITHASH) --build-arg STASH_VERSION=$(STASH_VERSION) -t stash/cuda-build -f docker/build/x86_64/Dockerfile-CUDA .
6 changes: 3 additions & 3 deletions docker/build/x86_64/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ RUN apk add --no-cache make
## cache node_modules separately
COPY ./ui/v2.5/package.json ./ui/v2.5/yarn.lock /stash/ui/v2.5/
WORKDIR /stash
RUN yarn --cwd ui/v2.5 install --frozen-lockfile.
RUN make pre-ui
COPY Makefile /stash/
COPY ./graphql /stash/graphql/
COPY ./ui /stash/ui/
RUN make generate-frontend
RUN make generate-ui
ARG GITHASH
ARG STASH_VERSION
RUN BUILD_DATE=$(date +"%Y-%m-%d %H:%M:%S") make ui
Expand All @@ -29,7 +29,7 @@ COPY --from=frontend /stash /stash/
RUN make generate-backend
ARG GITHASH
ARG STASH_VERSION
RUN make build
RUN make stash-release

# Final Runnable Image
FROM alpine:latest
Expand Down
4 changes: 2 additions & 2 deletions docker/build/x86_64/Dockerfile-CUDA
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN apk add --no-cache make
## cache node_modules separately
COPY ./ui/v2.5/package.json ./ui/v2.5/yarn.lock /stash/ui/v2.5/
WORKDIR /stash
RUN yarn --cwd ui/v2.5 install --frozen-lockfile.
RUN make pre-ui
COPY Makefile /stash/
COPY ./graphql /stash/graphql/
COPY ./ui /stash/ui/
Expand All @@ -29,7 +29,7 @@ COPY --from=frontend /stash /stash/
RUN make generate-backend
ARG GITHASH
ARG STASH_VERSION
RUN make build
RUN make stash-release

# Final Runnable Image
FROM nvidia/cuda:12.0.1-base-ubuntu22.04
Expand Down
Loading

0 comments on commit 96f2229

Please sign in to comment.