forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGNUmakefile
More file actions
163 lines (138 loc) · 6.13 KB
/
Copy pathGNUmakefile
File metadata and controls
163 lines (138 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# See https://googlecloudplatform.github.io/magic-modules/docs/getting-started/generate-providers/
# for a guide on provider generation.
default: build
# mm setup
ifneq ($(PRODUCT),)
mmv1_args=--product $(PRODUCT)
endif
ifneq ($(RESOURCE),)
mmv1_args += --resource $(RESOURCE)
endif
ifneq ($(OVERRIDES),)
mmv1_args += --overrides $(OVERRIDES)
endif
UNAME := $(shell uname)
# The inplace editing semantics are different between linux and osx.
ifeq ($(UNAME), Linux)
SED_I := -i
else
SED_I := -i '' -E
endif
SHOULD_SKIP_CLEAN := false # Default: do not skip
ifneq ($(SKIP_CLEAN),)
ifneq ($(SKIP_CLEAN),false)
SHOULD_SKIP_CLEAN := true
endif
endif
# Default to bazel if available (and not on GHA)
ifeq ($(GITHUB_ACTIONS),true)
_ := $(shell printf "\e[1;32mINFO:\e[0m Running in GitHub actions; not using bazel\n" 1>&2)
USE_BAZEL=0
else ifneq ($(shell which bazel),)
ifneq ($(USE_BAZEL), 0)
_ := $(shell printf "\e[1;32mINFO:\e[0m bazel detected; set USE_BAZEL=0 to disable bazel usage\n" 1>&2)
USE_BAZEL=1
endif
endif
ifeq ($(USE_BAZEL), 1)
MM_BINARY=../bazel-bin/mmv1/mmv1_/mmv1
else
_ := $(shell printf "\e[1;33mWARNING:\e[0m magic-modules will start requiring installation of bazel for builds in the near future. You can opt in now by installing bazel (recommended: use bazelisk): https://bazel.build/install\n" 1>&2)
MM_BINARY=../bin/mmv1
endif
terraform build provider: validate_environment clean-provider mmv1
@echo "Provider generation process finished for $(VERSION) in $(OUTPUT_PATH)"
mm_binary:
@echo "Building mmv1 binary";
@if [ "$(USE_BAZEL)" != "1" ]; then \
cd mmv1 && go build -o $(MM_BINARY) .; \
else \
bazel --version 2>&1 > /dev/null; \
correctVersion=$$?; \
required="$$(cat .bazelversion)"; \
if [ $${correctVersion} -eq 1 ]; then \
printf "\e[1;33mWARNING:\e[0m bazel $${required} is required and not found. See https://bazel.build/install for information on installing it. (Recommended: Use Bazelisk.)\n"; \
printf " Set USE_BAZEL=0 to disable bazel usage.\n"; \
exit 1; \
fi; \
bazel build //mmv1;\
fi
mmv1: mm_binary
@echo "Executing mmv1 build for $(OUTPUT_PATH)";
@cd mmv1;\
if [ "$(VERSION)" = "ga" ]; then \
$(MM_BINARY) --output $(OUTPUT_PATH) --version ga --no-docs $(mmv1_args) \
&& $(MM_BINARY) --output $(OUTPUT_PATH) --version beta --no-code $(mmv1_args); \
else \
$(MM_BINARY) --output $(OUTPUT_PATH) --version $(VERSION) $(mmv1_args); \
fi
clean-provider: check_safe_build
@if [ -n "$(PRODUCT)" ]; then \
printf "\n\e[1;33mWARNING:\e[0m Skipping clean-provider step because PRODUCT ('$(PRODUCT)') is set.\n"; \
printf " Ensure your downstream repository is synchronized with the Magic Modules branch\n"; \
printf " to avoid potential build inconsistencies.\n"; \
printf " Downstream repository (OUTPUT_PATH): %s\n\n" "$(OUTPUT_PATH)"; \
elif [ "$(SHOULD_SKIP_CLEAN)" = "true" ]; then \
printf "\e[1;33mINFO:\e[0m Skipping clean-provider step because SKIP_CLEAN is set to a non-false value ('$(SKIP_CLEAN)').\n"; \
else \
echo "Executing clean-provider in $(OUTPUT_PATH)..."; \
( \
cd $(OUTPUT_PATH) && \
echo "---> Changing directory to $(OUTPUT_PATH)" && \
if ! command -v git > /dev/null 2>&1; then \
printf "\e[1;33mINFO:\e[0m Skipping git-based cleaning because git is not installed.\n"; \
elif ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then \
printf "\e[1;33mINFO:\e[0m Skipping git-based cleaning because $(OUTPUT_PATH) is not a git repository.\n"; \
else \
echo "---> Downloading Go module dependencies... (Ensures tools like gofmt can find relevant code)" && \
go mod download && \
echo "---> Finding tracked files to remove..." && \
git ls-files | grep -v -E '(^\.git|^\.changelog|^\.travis\.yml$$|^\.golangci\.yml$$|^CHANGELOG\.md$$|^CHANGELOG_v.*\.md$$|^GNUmakefile$$|docscheck\.sh$$|^\.whitesource$$|^LICENSE$$|^CODEOWNERS$$|^README\.md$$|^\.go-version$$|^\.hashibot\.hcl$$|^go\.mod$$|^go\.sum$$|^examples|^scripts/)' | xargs -r git rm -f -q && \
echo "---> Unstaging changes with git reset..." && \
git reset -q && \
echo "---> clean-provider actions finished. Changes have been unstaged."; \
fi \
) && echo "clean-provider target finished successfully."; \
fi
clean-tgc:
cd $(OUTPUT_PATH);\
rm -rf ./tfplan2cai/testdata/templates/;\
rm -rf ./tfplan2cai/testdata/generatedconvert/;\
rm -rf ./tfplan2cai/converters/google/provider;\
rm -rf ./tfplan2cai/converters/google/resources;\
rm -rf ./cai2hcl/*;\
rm -rf ./tfplan2cai/test/*;\
rm -rf ./pkg/*;\
rm -rf ./test/*;\
rm -rf ./cmd/*;\
tgc: mm_binary
@cd mmv1;\
$(MM_BINARY) --version beta --provider tgc --output $(OUTPUT_PATH)/tfplan2cai $(mmv1_args)\
&& $(MM_BINARY) --version ga --provider tgc_cai2hcl --output $(OUTPUT_PATH)/cai2hcl $(mmv1_args)\
&& $(MM_BINARY) --version ga --provider tgc_next --output $(OUTPUT_PATH) $(mmv1_args);\
tf-oics: mm_binary
@cd mmv1;\
$(MM_BINARY) --version ga --provider oics --output $(OUTPUT_PATH) $(mmv1_args);\
test:
if [ "$(USE_BAZEL)" != "1" ]; then \
cd mmv1 && go test ./...; \
else \
bazel test //...;\
fi
validate_environment: check_parameters check_safe_build
check_parameters:
# only print doctor script to console if there was a dependency failure detected.
@./scripts/doctor 2>&1 > /dev/null || ./scripts/doctor
@[ -d "$(OUTPUT_PATH)" ] || (printf "\n\e[1;31mERROR: directory '$(OUTPUT_PATH)' does not exist - ENV variable \033[0mOUTPUT_PATH\e[1;31m should be set to a provider directory. \033[0m \n\n" && exit 1);
@[ -n "$(VERSION)" ] || (printf "\n\e[1;31mERROR: version '$(VERSION)' does not exist - ENV variable \033[0mVERSION\e[1;31m should be set to ga or beta \033[0m \n\n" && exit 1);
check_safe_build:
@([ -f "$(OUTPUT_PATH)/go.mod" ] && head -n 1 "$(OUTPUT_PATH)/go.mod" | grep -q 'terraform') || \
( \
printf "\n\e[1;31mERROR: Validation failed for OUTPUT_PATH '$(OUTPUT_PATH)'.\n" && \
printf " Either go.mod is missing or the module name within it does not contain 'terraform'.\n" && \
printf " This is a safety check before cleaning/building. Halting.\033[0m\n\n" && \
exit 1 \
); \
doctor:
./scripts/doctor
.PHONY: mmv1 test clean-provider validate_environment doctor