github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/Makefile (about)

     1  # Copyright 2014 The Cockroach Authors.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  # implied. See the License for the specific language governing
    13  # permissions and limitations under the License.
    14  
    15  # WARNING: This Makefile is not easily understood. If you're here looking for
    16  # typical Make invocations to build the project and run tests, you'll be better
    17  # served by running `make help`.
    18  #
    19  # Maintainers: the output of `make help` is automatically generated from the
    20  # double-hash (##) comments throughout this Makefile. Please submit
    21  # improvements!
    22  
    23  # We need to define $(GO) early because it's needed for defs.mk.
    24  GO      ?= go
    25  # xgo is needed also for defs.mk.
    26  override xgo := GOFLAGS= $(GO)
    27  
    28  # defs.mk stores cached values of shell commands to avoid recomputing them on
    29  # every Make invocation. This has a small but noticeable effect, especially on
    30  # noop builds.
    31  # This needs to be the first rule because we're including build/defs.mk
    32  # first thing below, and Make needs to know how to build it.
    33  .SECONDARY: build/defs.mk
    34  build/defs.mk: Makefile build/defs.mk.sig remove_obsolete_execgen
    35  ifndef IGNORE_GOVERS
    36  	@build/go-version-check.sh $(GO) || { echo "Disable this check with IGNORE_GOVERS=1." >&2; exit 1; }
    37  endif
    38  	@echo "macos-version = $$(sw_vers -productVersion 2>/dev/null | grep -oE '[0-9]+\.[0-9]+')" > $@.tmp
    39  	@echo "GOEXE = $$($(xgo) env GOEXE)" >> $@.tmp
    40  	@echo "NCPUS = $$({ getconf _NPROCESSORS_ONLN || sysctl -n hw.ncpu || nproc; } 2>/dev/null)" >> $@.tmp
    41  	@echo "UNAME = $$(uname)" >> $@.tmp
    42  	@echo "HOST_TRIPLE = $$($$($(GO) env CC) -dumpmachine)" >> $@.tmp
    43  	@echo "GIT_DIR = $$(git rev-parse --git-dir 2>/dev/null)" >> $@.tmp
    44  	@echo "GITHOOKSDIR = $$(test -d .git && echo '.git/hooks' || git rev-parse --git-path hooks)" >> $@.tmp
    45  	@echo "have-defs = 1" >> $@.tmp
    46  	@set -e; \
    47  	if ! cmp -s $@.tmp $@; then \
    48  	   mv -f $@.tmp $@; \
    49  	   echo "Detected change in build system. Rebooting make." >&2; \
    50  	else rm -f $@.tmp; fi
    51  
    52  include build/defs.mk
    53  
    54  # Nearly everything below this point needs to have the vendor directory ready
    55  # for use and will depend on bin/.submodules-initialized. In order to
    56  # ensure this is available before the first "include" directive depending
    57  # on it, we'll have it listed first thing.
    58  #
    59  # Note how the actions for this rule are *not* using $(GIT_DIR) which
    60  # is otherwise defined in defs.mk above. This is because submodules
    61  # are used in the process of definining the .mk files included by the
    62  # Makefile, so it is not yet defined by the time
    63  # `.submodules-initialized` is needed during a fresh build after a
    64  # checkout.
    65  .SECONDARY: bin/.submodules-initialized
    66  bin/.submodules-initialized:
    67  	gitdir=$$(git rev-parse --git-dir 2>/dev/null || true); \
    68  	if test -n "$$gitdir"; then \
    69  	   git submodule update --init --recursive; \
    70  	fi
    71  	mkdir -p $(@D)
    72  	touch $@
    73  
    74  # If the user wants to persist customizations for some variables, they
    75  # can do so by defining `customenv.mk` in their work tree.
    76  -include customenv.mk
    77  
    78  ifeq "$(findstring bench,$(MAKECMDGOALS))" "bench"
    79  $(if $(TESTS),$(error TESTS cannot be specified with `make bench` (did you mean BENCHES?)))
    80  else
    81  $(if $(BENCHES),$(error BENCHES can only be specified with `make bench`))
    82  endif
    83  
    84  # Prevent invoking make with a specific test name without a constraining
    85  # package.
    86  ifneq "$(filter bench% test% stress%,$(MAKECMDGOALS))" ""
    87  ifeq "$(PKG)" ""
    88  $(if $(subst -,,$(TESTS)),$(error TESTS must be specified with PKG (e.g. PKG=./pkg/sql)))
    89  $(if $(subst -,,$(BENCHES)),$(error BENCHES must be specified with PKG (e.g. PKG=./pkg/sql)))
    90  endif
    91  endif
    92  
    93  TYPE :=
    94  ifneq "$(TYPE)" ""
    95  $(error Make no longer understands TYPE. Use 'build/builder.sh mkrelease $(subst release-,,$(TYPE))' instead)
    96  endif
    97  
    98  # dep-build is set to non-empty if the .d files should be included.
    99  # This definition makes it empty when only the targets "help" and/or "clean"
   100  # are specified.
   101  build-with-dep-files := $(or $(if $(MAKECMDGOALS),,implicit-all),$(filter-out help clean,$(MAKECMDGOALS)))
   102  
   103  ## Which package to run tests against, e.g. "./pkg/foo".
   104  PKG := ./pkg/...
   105  
   106  ## Tests to run for use with `make test` or `make check-libroach`.
   107  TESTS := .
   108  
   109  ## Benchmarks to run for use with `make bench`.
   110  BENCHES :=
   111  
   112  ## Space delimited list of logic test files to run, for make testlogic/testccllogic/testoptlogic.
   113  FILES :=
   114  
   115  ## Name of a logic test configuration to run, for make testlogic/testccllogic/testoptlogic.
   116  ## (default: all configs. It's not possible yet to specify multiple configs in this way.)
   117  TESTCONFIG :=
   118  
   119  ## Regex for matching logic test subtests. This is always matched after "FILES"
   120  ## if they are provided.
   121  SUBTESTS :=
   122  
   123  ## Test timeout to use for the linter.
   124  LINTTIMEOUT := 20m
   125  
   126  ## Test timeout to use for regular tests.
   127  TESTTIMEOUT := 30m
   128  
   129  ## Test timeout to use for race tests.
   130  RACETIMEOUT := 30m
   131  
   132  ## Test timeout to use for acceptance tests.
   133  ACCEPTANCETIMEOUT := 30m
   134  
   135  ## Test timeout to use for benchmarks.
   136  BENCHTIMEOUT := 5m
   137  
   138  ## Extra flags to pass to the go test runner, e.g. "-v --vmodule=raft=1"
   139  TESTFLAGS :=
   140  
   141  ## Flags to pass to `go test` invocations that actually run tests, but not
   142  ## elsewhere. Used for the -json flag which we'll only want to pass
   143  ## selectively.  There's likely a better solution.
   144  GOTESTFLAGS :=
   145  
   146  ## Extra flags to pass to `stress` during `make stress`.
   147  STRESSFLAGS :=
   148  
   149  ## Cluster to use for `make roachprod-stress`
   150  CLUSTER :=
   151  
   152  ## Verbose allows turning on verbose output from the cmake builds.
   153  VERBOSE :=
   154  
   155  ## Indicate the base root directory where to install
   156  DESTDIR :=
   157  
   158  DUPLFLAGS    := -t 100
   159  GOFLAGS      :=
   160  TAGS         :=
   161  ARCHIVE      := cockroach.src.tgz
   162  STARTFLAGS   := -s type=mem,size=1GiB --logtostderr
   163  BUILDTARGET  := ./pkg/cmd/cockroach
   164  SUFFIX       := $(GOEXE)
   165  INSTALL      := install
   166  prefix       := /usr/local
   167  bindir       := $(prefix)/bin
   168  
   169  ifeq "$(findstring -j,$(shell ps -o args= $$PPID))" ""
   170  ifdef NCPUS
   171  MAKEFLAGS += -j$(NCPUS)
   172  $(info Running make with -j$(NCPUS))
   173  endif
   174  endif
   175  
   176  help: ## Print help for targets with comments.
   177  	@echo "Usage:"
   178  	@echo "  make [target...] [VAR=foo VAR2=bar...]"
   179  	@echo ""
   180  	@echo "Useful commands:"
   181  	@grep -Eh '^[a-zA-Z._-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(cyan)%-30s$(term-reset) %s\n", $$1, $$2}'
   182  	@echo ""
   183  	@echo "Useful variables:"
   184  	@awk 'BEGIN { FS = ":=" } /^## /{x = substr($$0, 4); getline; if (NF >= 2) printf "  $(cyan)%-30s$(term-reset) %s\n", $$1, x}' $(MAKEFILE_LIST) | sort
   185  	@echo ""
   186  	@echo "Typical usage:"
   187  	@printf "  $(cyan)%s$(term-reset)\n    %s\n\n" \
   188  		"make test" "Run all unit tests." \
   189  		"make test PKG=./pkg/sql" "Run all unit tests in the ./pkg/sql package" \
   190  		"make test PKG=./pkg/sql/parser TESTS=TestParse" "Run the TestParse test in the ./pkg/sql/parser package." \
   191  		"make bench PKG=./pkg/sql/parser BENCHES=BenchmarkParse" "Run the BenchmarkParse benchmark in the ./pkg/sql/parser package." \
   192  		"make testlogic" "Run all OSS SQL logic tests." \
   193  		"make testccllogic" "Run all CCL SQL logic tests." \
   194  		"make testoptlogic" "Run all opt exec builder SQL logic tests." \
   195  		"make testbaselogic" "Run all the baseSQL SQL logic tests." \
   196  		"make testlogic FILES='prepare|fk'" "Run the logic tests in the files named prepare and fk (the full path is not required)." \
   197  		"make testlogic FILES=fk SUBTESTS='20042|20045'" "Run the logic tests within subtests 20042 and 20045 in the file named fk." \
   198  		"make testlogic TESTCONFIG=local" "Run the logic tests for the cluster configuration 'local'." \
   199  		"make fuzz" "Run all fuzz tests for 12m each (or whatever the default TESTTIMEOUT is)." \
   200  		"make fuzz PKG=./pkg/sql/... TESTTIMEOUT=1m" "Run all fuzz tests under the sql directory for 1m each." \
   201  		"make fuzz PKG=./pkg/sql/sem/tree TESTS=Decimal TESTTIMEOUT=1m" "Run the Decimal fuzz tests in the tree directory for 1m." \
   202  		"make check-libroach TESTS=ccl" "Run the libroach tests matching .*ccl.*"
   203  
   204  BUILDTYPE := development
   205  
   206  # Build C/C++ with basic debugging information.
   207  CFLAGS += -g1
   208  CXXFLAGS += -g1
   209  LDFLAGS ?=
   210  
   211  # TODO(benesch): remove filter-outs below when golang/go#26144 and
   212  # golang/go#16651, respectively, are fixed.
   213  CGO_CFLAGS = $(filter-out -g%,$(CFLAGS))
   214  CGO_CXXFLAGS = $(CXXFLAGS)
   215  CGO_LDFLAGS = $(filter-out -static,$(LDFLAGS))
   216  
   217  export CFLAGS CXXFLAGS LDFLAGS CGO_CFLAGS CGO_CXXFLAGS CGO_LDFLAGS
   218  
   219  # We intentionally use LINKFLAGS instead of the more traditional LDFLAGS
   220  # because LDFLAGS has built-in semantics that don't make sense with the Go
   221  # toolchain.
   222  override LINKFLAGS = -X github.com/cockroachdb/cockroach/pkg/build.typ=$(BUILDTYPE) -extldflags "$(LDFLAGS)"
   223  
   224  GOFLAGS ?=
   225  TAR     ?= tar
   226  
   227  # Ensure we have an unambiguous GOPATH.
   228  GOPATH := $(shell $(GO) env GOPATH)
   229  
   230  ifneq "$(or $(findstring :,$(GOPATH)),$(findstring ;,$(GOPATH)))" ""
   231  $(error GOPATHs with multiple entries are not supported)
   232  endif
   233  
   234  GOPATH := $(realpath $(GOPATH))
   235  ifeq "$(strip $(GOPATH))" ""
   236  $(error GOPATH is not set and could not be automatically determined)
   237  endif
   238  
   239  ifeq "$(filter $(GOPATH)%,$(CURDIR))" ""
   240  $(error Current directory "$(CURDIR)" is not within GOPATH "$(GOPATH)")
   241  endif
   242  
   243  ifeq "$(GOPATH)" "/"
   244  $(error GOPATH=/ is not supported)
   245  endif
   246  
   247  $(info GOPATH set to $(GOPATH))
   248  
   249  # We install our vendored tools to a directory within this repository to avoid
   250  # overwriting any user-installed binaries of the same name in the default GOBIN.
   251  GO_INSTALL := GOBIN='$(abspath bin)' GOFLAGS= $(GO) install
   252  
   253  # Prefer tools we've installed with go install and Yarn to those elsewhere on
   254  # the PATH.
   255  export PATH := $(abspath bin):$(PATH)
   256  
   257  # HACK: Make has a fast path and a slow path for command execution,
   258  # but the fast path uses the PATH variable from when make was started,
   259  # not the one we set on the previous line. In order for the above
   260  # line to have any effect, we must force make to always take the slow path.
   261  # Setting the SHELL variable to a value other than the default (/bin/sh)
   262  # is one way to do this globally.
   263  # http://stackoverflow.com/questions/8941110/how-i-could-add-dir-to-path-in-makefile/13468229#13468229
   264  #
   265  # We also force the PWD environment variable to $(CURDIR), which ensures that
   266  # any programs invoked by Make see a physical CWD without any symlinks. The Go
   267  # toolchain does not support symlinks well (for one example, see
   268  # https://github.com/golang/go/issues/24359). This may be fixed when GOPATH is
   269  # deprecated, so revisit whether this workaround is necessary then.
   270  export SHELL := env PWD=$(CURDIR) bash
   271  ifeq ($(SHELL),)
   272  $(error bash is required)
   273  endif
   274  
   275  # Invocation of any NodeJS script should be prefixed by NODE_RUN. See the
   276  # comments within node-run.sh for rationale.
   277  NODE_RUN := build/node-run.sh
   278  
   279  # make-lazy converts a recursive variable, which is evaluated every time it's
   280  # referenced, to a lazy variable, which is evaluated only the first time it's
   281  # used. See: http://blog.jgc.org/2016/07/lazy-gnu-make-variables.html
   282  override make-lazy = $(eval $1 = $$(eval $1 := $(value $1))$$($1))
   283  
   284  # GNU tar and BSD tar both support transforming filenames according to a regular
   285  # expression, but have different flags to do so.
   286  TAR_XFORM_FLAG = $(shell $(TAR) --version | grep -q GNU && echo "--xform='flags=r;s'" || echo "-s")
   287  $(call make-lazy,TAR_XFORM_FLAG)
   288  
   289  # To edit in-place without creating a backup file, GNU sed requires a bare -i,
   290  # while BSD sed requires an empty string as the following argument.
   291  SED_INPLACE = sed $(shell sed --version 2>&1 | grep -q GNU && echo -i || echo "-i ''")
   292  $(call make-lazy,SED_INPLACE)
   293  
   294  # MAKE_TERMERR is set automatically in Make v4.1+, but macOS is still shipping
   295  # v3.81.
   296  MAKE_TERMERR ?= $(shell [[ -t 2 ]] && echo true)
   297  
   298  # This is how you get a literal space into a Makefile.
   299  space := $(eval) $(eval)
   300  
   301  # Color support.
   302  yellow = $(shell { tput setaf 3 || tput AF 3; } 2>/dev/null)
   303  cyan = $(shell { tput setaf 6 || tput AF 6; } 2>/dev/null)
   304  term-reset = $(shell { tput sgr0 || tput me; } 2>/dev/null)
   305  $(call make-lazy,yellow)
   306  $(call make-lazy,cyan)
   307  $(call make-lazy,term-reset)
   308  
   309  # Tell Make to delete the target if its recipe fails. Otherwise, if a recipe
   310  # modifies its target before failing, the target's timestamp will make it appear
   311  # up-to-date on the next invocation of Make, even though it is likely corrupt.
   312  # See: https://www.gnu.org/software/make/manual/html_node/Errors.html#Errors
   313  .DELETE_ON_ERROR:
   314  
   315  # Targets that name a real file that must be rebuilt on every Make invocation
   316  # should depend on .ALWAYS_REBUILD. (.PHONY should only be used on targets that
   317  # don't name a real file because .DELETE_ON_ERROR does not apply to .PHONY
   318  # targets.)
   319  .ALWAYS_REBUILD:
   320  .PHONY: .ALWAYS_REBUILD
   321  
   322  ifneq ($(GIT_DIR),)
   323  # If we're in a git worktree, the git hooks directory may not be in our root,
   324  # so we ask git for the location.
   325  #
   326  # Note that `git rev-parse --git-path hooks` requires git 2.5+.
   327  GITHOOKS := $(subst githooks/,$(GITHOOKSDIR)/,$(wildcard githooks/*))
   328  $(GITHOOKSDIR)/%: githooks/%
   329  	@echo installing $<
   330  	@rm -f $@
   331  	@mkdir -p $(dir $@)
   332  	@ln -s ../../$(basename $<) $(dir $@)
   333  endif
   334  
   335  .SECONDARY: pkg/ui/yarn.installed
   336  pkg/ui/yarn.installed: pkg/ui/package.json pkg/ui/yarn.lock pkg/ui/yarn.protobufjs-cli.lock | bin/.submodules-initialized
   337  	$(NODE_RUN) -C pkg/ui yarn install --offline
   338  	# Prevent ProtobufJS from trying to install its own packages because a) the
   339  	# the feature is buggy, and b) it introduces an unnecessary dependency on NPM.
   340  	# See: https://github.com/dcodeIO/protobuf.js/issues/716.
   341  	# We additionally pin the dependencies by linking in a lock file for
   342  	# reproducable builds.
   343  	$(NODE_RUN) pkg/ui/bin/gen-protobuf-cli-deps.js > pkg/ui/node_modules/protobufjs/cli/package.json
   344  	ln -sf ../../../yarn.protobufjs-cli.lock pkg/ui/node_modules/protobufjs/cli/yarn.lock
   345  	$(NODE_RUN) -C pkg/ui/node_modules/protobufjs/cli yarn install --offline
   346  	@# We remove this broken dependency again in pkg/ui/webpack.config.js.
   347  	@# See the comment there for details.
   348  	rm -rf pkg/ui/node_modules/@types/node
   349  	touch $@
   350  
   351  # Update the git hooks and install commands from dependencies whenever they
   352  # change.
   353  bin/.bootstrap: $(GITHOOKS) Gopkg.lock | bin/.submodules-initialized
   354  	@$(GO_INSTALL) -v \
   355  		./vendor/github.com/client9/misspell/cmd/misspell \
   356  		./vendor/github.com/cockroachdb/crlfmt \
   357  		./vendor/github.com/cockroachdb/gostdlib/cmd/gofmt \
   358  		./vendor/github.com/cockroachdb/gostdlib/x/tools/cmd/goimports \
   359  		./vendor/github.com/cockroachdb/stress \
   360  		./vendor/github.com/golang/dep/cmd/dep \
   361  		./vendor/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
   362  		./vendor/github.com/kevinburke/go-bindata/go-bindata \
   363  		./vendor/github.com/kisielk/errcheck \
   364  		./vendor/github.com/mattn/goveralls \
   365  		./vendor/github.com/mibk/dupl \
   366  		./vendor/github.com/mmatczuk/go_generics/cmd/go_generics \
   367  		./vendor/github.com/wadey/gocovmerge \
   368  		./vendor/golang.org/x/lint/golint \
   369  		./vendor/golang.org/x/perf/cmd/benchstat \
   370  		./vendor/golang.org/x/tools/cmd/goyacc \
   371  		./vendor/golang.org/x/tools/cmd/stringer \
   372  		./vendor/golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow \
   373  		./vendor/honnef.co/go/tools/cmd/staticcheck
   374  	touch $@
   375  
   376  IGNORE_GOVERS :=
   377  
   378  # The following section handles building our C/C++ dependencies. These are
   379  # common because both the root Makefile and protobuf.mk have C dependencies.
   380  
   381  host-is-macos := $(findstring Darwin,$(UNAME))
   382  host-is-mingw := $(findstring MINGW,$(UNAME))
   383  
   384  ifdef host-is-macos
   385  # On macOS 10.11, XCode SDK v8.1 (and possibly others) indicate the presence of
   386  # symbols that don't exist until macOS 10.12. Setting MACOSX_DEPLOYMENT_TARGET
   387  # to the host machine's actual macOS version works around this. See:
   388  # https://github.com/jemalloc/jemalloc/issues/494.
   389  export MACOSX_DEPLOYMENT_TARGET ?= $(macos-version)
   390  endif
   391  
   392  # Cross-compilation occurs when you set TARGET_TRIPLE to something other than
   393  # HOST_TRIPLE. You'll need to ensure the cross-compiling toolchain is on your
   394  # path and override the rest of the variables that immediately follow as
   395  # necessary. For an example, see build/builder/cmd/mkrelease, which sets these
   396  # variables appropriately for the toolchains baked into the builder image.
   397  TARGET_TRIPLE := $(HOST_TRIPLE)
   398  XCMAKE_SYSTEM_NAME :=
   399  XGOOS :=
   400  XGOARCH :=
   401  XCC := $(TARGET_TRIPLE)-cc
   402  XCXX := $(TARGET_TRIPLE)-c++
   403  EXTRA_XCMAKE_FLAGS :=
   404  EXTRA_XCONFIGURE_FLAGS :=
   405  
   406  ifneq ($(HOST_TRIPLE),$(TARGET_TRIPLE))
   407  is-cross-compile := 1
   408  endif
   409  
   410  target-is-windows := $(findstring w64,$(TARGET_TRIPLE))
   411  
   412  # CMAKE_TARGET_MESSAGES=OFF prevents CMake from printing progress messages
   413  # whenever a target is fully built to prevent spammy output from make when
   414  # c-deps are all already built. Progress messages are still printed when actual
   415  # compilation is being performed.
   416  cmake-flags := -DCMAKE_TARGET_MESSAGES=OFF $(if $(host-is-mingw),-G 'MSYS Makefiles')
   417  configure-flags :=
   418  
   419  # Use xcmake-flags when invoking CMake on libraries/binaries for the target
   420  # platform (i.e., the cross-compiled platform, if specified); use plain
   421  # cmake-flags when invoking CMake on libraries/binaries for the host platform.
   422  # Similarly for xconfigure-flags and configure-flags, and xgo and GO.
   423  xcmake-flags := $(cmake-flags) $(EXTRA_XCMAKE_FLAGS)
   424  xconfigure-flags := $(configure-flags) $(EXTRA_XCONFIGURE_FLAGS)
   425  
   426  # If we're cross-compiling, inform Autotools and CMake.
   427  ifdef is-cross-compile
   428  xconfigure-flags += --host=$(TARGET_TRIPLE) CC=$(XCC) CXX=$(XCXX)
   429  xcmake-flags += -DCMAKE_SYSTEM_NAME=$(XCMAKE_SYSTEM_NAME) -DCMAKE_C_COMPILER=$(XCC) -DCMAKE_CXX_COMPILER=$(XCXX)
   430  override xgo := GOFLAGS= GOOS=$(XGOOS) GOARCH=$(XGOARCH) CC=$(XCC) CXX=$(XCXX) $(xgo)
   431  endif
   432  
   433  C_DEPS_DIR := $(abspath c-deps)
   434  CRYPTOPP_SRC_DIR := $(C_DEPS_DIR)/cryptopp
   435  JEMALLOC_SRC_DIR := $(C_DEPS_DIR)/jemalloc
   436  PROTOBUF_SRC_DIR := $(C_DEPS_DIR)/protobuf
   437  ROCKSDB_SRC_DIR  := $(C_DEPS_DIR)/rocksdb
   438  SNAPPY_SRC_DIR   := $(C_DEPS_DIR)/snappy
   439  GEOS_SRC_DIR     := $(C_DEPS_DIR)/geos
   440  PROJ_SRC_DIR     := $(C_DEPS_DIR)/proj
   441  LIBEDIT_SRC_DIR  := $(C_DEPS_DIR)/libedit
   442  LIBROACH_SRC_DIR := $(C_DEPS_DIR)/libroach
   443  KRB5_SRC_DIR     := $(C_DEPS_DIR)/krb5
   444  
   445  # Derived build variants.
   446  use-stdmalloc          := $(findstring stdmalloc,$(TAGS))
   447  use-msan               := $(findstring msan,$(GOFLAGS))
   448  
   449  # User-requested build variants.
   450  ENABLE_LIBROACH_ASSERTIONS ?=
   451  ENABLE_ROCKSDB_ASSERTIONS ?=
   452  
   453  BUILD_DIR := $(GOPATH)/native/$(TARGET_TRIPLE)
   454  
   455  # In MinGW, cgo flags don't handle Unix-style paths, so convert our base path to
   456  # a Windows-style path.
   457  #
   458  # TODO(benesch): Figure out why. MinGW transparently converts Unix-style paths
   459  # everywhere else.
   460  ifdef host-is-mingw
   461  BUILD_DIR := $(shell cygpath -m $(BUILD_DIR))
   462  endif
   463  
   464  CRYPTOPP_DIR := $(BUILD_DIR)/cryptopp$(if $(use-msan),_msan)
   465  JEMALLOC_DIR := $(BUILD_DIR)/jemalloc$(if $(use-msan),_msan)
   466  PROTOBUF_DIR := $(BUILD_DIR)/protobuf$(if $(use-msan),_msan)
   467  ROCKSDB_DIR  := $(BUILD_DIR)/rocksdb$(if $(use-msan),_msan)$(if $(use-stdmalloc),_stdmalloc)$(if $(ENABLE_ROCKSDB_ASSERTIONS),_assert)
   468  SNAPPY_DIR   := $(BUILD_DIR)/snappy$(if $(use-msan),_msan)
   469  GEOS_DIR     := $(BUILD_DIR)/geos$(if $(use-msan),_msan)
   470  PROJ_DIR     := $(BUILD_DIR)/proj$(if $(use-msan),_msan)
   471  LIBEDIT_DIR  := $(BUILD_DIR)/libedit$(if $(use-msan),_msan)
   472  LIBROACH_DIR := $(BUILD_DIR)/libroach$(if $(use-msan),_msan)$(if $(ENABLE_LIBROACH_ASSERTIONS),_assert)
   473  KRB5_DIR     := $(BUILD_DIR)/krb5$(if $(use-msan),_msan)
   474  # Can't share with protobuf because protoc is always built for the host.
   475  PROTOC_DIR := $(GOPATH)/native/$(HOST_TRIPLE)/protobuf
   476  
   477  LIBCRYPTOPP := $(CRYPTOPP_DIR)/libcryptopp.a
   478  LIBJEMALLOC := $(JEMALLOC_DIR)/lib/libjemalloc.a
   479  LIBPROTOBUF := $(PROTOBUF_DIR)/libprotobuf.a
   480  LIBROCKSDB  := $(ROCKSDB_DIR)/librocksdb.a
   481  LIBSNAPPY   := $(SNAPPY_DIR)/libsnappy.a
   482  LIBEDIT     := $(LIBEDIT_DIR)/src/.libs/libedit.a
   483  LIBROACH    := $(LIBROACH_DIR)/libroach.a
   484  LIBROACHCCL := $(LIBROACH_DIR)/libroachccl.a
   485  LIBPROJ     := $(PROJ_DIR)/lib/libproj.a
   486  LIBKRB5     := $(KRB5_DIR)/lib/libgssapi_krb5.a
   487  PROTOC      := $(PROTOC_DIR)/protoc
   488  
   489  DYN_LIB_DIR := lib
   490  DYN_EXT     := so
   491  ifdef host-is-macos
   492  DYN_EXT     := dylib
   493  endif
   494  
   495  LIBGEOS     := $(DYN_LIB_DIR)/libgeos.$(DYN_EXT)
   496  
   497  C_LIBS_COMMON = \
   498  	$(if $(use-stdmalloc),,$(LIBJEMALLOC)) \
   499  	$(if $(target-is-windows),,$(LIBEDIT) $(LIBGEOS)) \
   500  	$(LIBPROTOBUF) $(LIBSNAPPY) $(LIBROCKSDB) $(LIBPROJ)
   501  C_LIBS_OSS = $(C_LIBS_COMMON) $(LIBROACH)
   502  C_LIBS_CCL = $(C_LIBS_COMMON) $(LIBCRYPTOPP) $(LIBROACHCCL)
   503  
   504  # We only include krb5 on linux, non-musl builds.
   505  ifeq "$(findstring linux-gnu,$(TARGET_TRIPLE))" "linux-gnu"
   506  C_LIBS_CCL += $(LIBKRB5)
   507  KRB_CPPFLAGS := $(KRB5_DIR)/include
   508  KRB_DIR := $(KRB5_DIR)/lib
   509  override TAGS += gss
   510  endif
   511  
   512  # Go does not permit dashes in build tags. This is undocumented.
   513  native-tag := $(subst -,_,$(TARGET_TRIPLE))$(if $(use-stdmalloc),_stdmalloc)$(if $(use-msan),_msan)
   514  
   515  # In each package that uses cgo, we inject include and library search paths into
   516  # files named zcgo_flags_{native-tag}.go. The logic for this is complicated so
   517  # that Make-driven builds can cache the state of builds for multiple
   518  # configurations at once, while still allowing the use of `go build` and `go
   519  # test` for the configuration most recently built with Make.
   520  #
   521  # Building with Make always adds the `make` and {native-tag} tags to the build.
   522  #
   523  # Unsuffixed flags files (zcgo_flags.cgo) have the build constraint `!make` and
   524  # are only compiled when invoking the Go toolchain directly on a package-- i.e.,
   525  # when the `make` build tag is not specified. These files are rebuilt whenever
   526  # the build signature changes (see build/defs.mk.sig), and so reflect the target
   527  # triple that Make was most recently invoked with.
   528  #
   529  # Suffixed flags files (e.g. zcgo_flags_{native-tag}.go) have the build
   530  # constraint `{native-tag}` and are built the first time a Make-driven build
   531  # encounters a given native tag or when the build signature changes (see
   532  # build/defs.mk.sig). These tags are unset when building with the Go toolchain
   533  # directly, so these files are only compiled when building with Make.
   534  CGO_PKGS := \
   535  	pkg/cli \
   536  	pkg/server/status \
   537  	pkg/storage \
   538  	pkg/ccl/storageccl/engineccl \
   539  	pkg/ccl/gssapiccl \
   540  	pkg/geo/geoproj \
   541  	vendor/github.com/knz/go-libedit/unix
   542  vendor/github.com/knz/go-libedit/unix-package := libedit_unix
   543  CGO_UNSUFFIXED_FLAGS_FILES := $(addprefix ./,$(addsuffix /zcgo_flags.go,$(CGO_PKGS)))
   544  CGO_SUFFIXED_FLAGS_FILES   := $(addprefix ./,$(addsuffix /zcgo_flags_$(native-tag).go,$(CGO_PKGS)))
   545  BASE_CGO_FLAGS_FILES := $(CGO_UNSUFFIXED_FLAGS_FILES) $(CGO_SUFFIXED_FLAGS_FILES)
   546  CGO_FLAGS_FILES := $(BASE_CGO_FLAGS_FILES) vendor/github.com/knz/go-libedit/unix/zcgo_flags_extra.go
   547  
   548  $(BASE_CGO_FLAGS_FILES): Makefile build/defs.mk.sig | bin/.submodules-initialized
   549  	@echo "regenerating $@"
   550  	@echo '// GENERATED FILE DO NOT EDIT' > $@
   551  	@echo >> $@
   552  	@echo '// +build $(if $(findstring $(native-tag),$@),$(native-tag),!make)' >> $@
   553  	@echo >> $@
   554  	@echo 'package $(if $($(@D)-package),$($(@D)-package),$(notdir $(@D)))' >> $@
   555  	@echo >> $@
   556  	@echo '// #cgo CPPFLAGS: $(addprefix -I,$(JEMALLOC_DIR)/include $(KRB_CPPFLAGS) $(GEOS_DIR)/capi $(PROJ_DIR)/lib)' >> $@
   557  	@echo '// #cgo LDFLAGS: $(addprefix -L,$(CRYPTOPP_DIR) $(PROTOBUF_DIR) $(JEMALLOC_DIR)/lib $(SNAPPY_DIR) $(LIBEDIT_DIR)/src/.libs $(ROCKSDB_DIR) $(LIBROACH_DIR) $(KRB_DIR) $(PROJ_DIR)/lib)' >> $@
   558  	@echo 'import "C"' >> $@
   559  
   560  vendor/github.com/knz/go-libedit/unix/zcgo_flags_extra.go: Makefile | bin/.submodules-initialized
   561  	@echo "regenerating $@"
   562  	@echo '// GENERATED FILE DO NOT EDIT' > $@
   563  	@echo >> $@
   564  	@echo 'package $($(@D)-package)' >> $@
   565  	@echo >> $@
   566  	@echo '// #cgo CPPFLAGS: -DGO_LIBEDIT_NO_BUILD' >> $@
   567  	@echo '// #cgo !windows LDFLAGS: -ledit -lncurses' >> $@
   568  	@echo 'import "C"' >> $@
   569  
   570  # BUILD ARTIFACT CACHING
   571  #
   572  # We need to ensure that changes to a dependency's configure or CMake flags
   573  # below cause the corresponding dependency to be rebuilt. It would be correct to
   574  # have the dependencies list this file itself as a prerequisite, but *all*
   575  # dependencies would be rebuilt, likely unnecessarily, whenever this file
   576  # changed. Instead, we give each dependency its own marker file, DEP-rebuild, as
   577  # a prerequisite.
   578  #
   579  # It's not important *what* goes in the marker file, so long as its contents
   580  # change in the same commit as the configure flags. This causes Git to touch the
   581  # marker file when switching between revisions that span the change. For
   582  # simplicity, just sequentially bump the version number within.
   583  #
   584  # NB: the recipes below nuke *all* build artifacts when a dependency's configure
   585  # flags change. In theory, we could rely on the dependency's build system to
   586  # only rebuild the affected objects, but in practice dependencies on configure
   587  # flags are not tracked correctly, and these stale artifacts can cause
   588  # particularly hard-to-debug errors.
   589  #
   590  # Flags needed to make cryptopp to runtime detection of AES cpu instruction sets.
   591  # pclmul and ssse3 need to be defined for the overall AES switch but are only used
   592  # in GCM mode (not currently in use by cockroach).
   593  $(CRYPTOPP_DIR)/Makefile: aes := $(if $(findstring x86_64,$(TARGET_TRIPLE)),-maes -mpclmul -mssse3)
   594  $(CRYPTOPP_DIR)/Makefile: $(C_DEPS_DIR)/cryptopp-rebuild | bin/.submodules-initialized
   595  	rm -rf $(CRYPTOPP_DIR)
   596  	mkdir -p $(CRYPTOPP_DIR)
   597  	@# NOTE: If you change the CMake flags below, bump the version in
   598  	@# $(C_DEPS_DIR)/cryptopp-rebuild. See above for rationale.
   599  	cd $(CRYPTOPP_DIR) && CFLAGS+=" $(aes)" && CXXFLAGS+=" $(aes)" cmake $(xcmake-flags) $(CRYPTOPP_SRC_DIR) \
   600  	  -DCMAKE_BUILD_TYPE=Release
   601  
   602  $(JEMALLOC_SRC_DIR)/configure.ac: | bin/.submodules-initialized
   603  
   604  $(JEMALLOC_SRC_DIR)/configure: $(JEMALLOC_SRC_DIR)/configure.ac
   605  	cd $(JEMALLOC_SRC_DIR) && autoconf
   606  
   607  $(JEMALLOC_DIR)/Makefile: $(C_DEPS_DIR)/jemalloc-rebuild $(JEMALLOC_SRC_DIR)/configure
   608  	rm -rf $(JEMALLOC_DIR)
   609  	mkdir -p $(JEMALLOC_DIR)
   610  	@# NOTE: If you change the configure flags below, bump the version in
   611  	@# $(C_DEPS_DIR)/jemalloc-rebuild. See above for rationale.
   612  	cd $(JEMALLOC_DIR) && $(JEMALLOC_SRC_DIR)/configure $(xconfigure-flags) --enable-prof
   613  
   614  $(KRB5_SRC_DIR)/src/configure.in: | bin/.submodules-initialized
   615  
   616  $(KRB5_SRC_DIR)/src/configure: $(KRB5_SRC_DIR)/src/configure.in
   617  	cd $(KRB5_SRC_DIR)/src && autoreconf
   618  
   619  $(KRB5_DIR)/Makefile: $(C_DEPS_DIR)/krb5-rebuild $(KRB5_SRC_DIR)/src/configure
   620  	rm -rf $(KRB5_DIR)
   621  	mkdir -p $(KRB5_DIR)
   622  	@# NOTE: If you change the configure flags below, bump the version in
   623  	@# $(C_DEPS_DIR)/krb5-rebuild. See above for rationale.
   624  	@# If CFLAGS is set to -g1 then make will fail. Use "env -" to clear the environment.
   625  	cd $(KRB5_DIR) && env -u CFLAGS -u CXXFLAGS $(KRB5_SRC_DIR)/src/configure $(xconfigure-flags) --enable-static --disable-shared
   626  
   627  $(PROTOBUF_DIR)/Makefile: $(C_DEPS_DIR)/protobuf-rebuild | bin/.submodules-initialized
   628  	rm -rf $(PROTOBUF_DIR)
   629  	mkdir -p $(PROTOBUF_DIR)
   630  	@# NOTE: If you change the CMake flags below, bump the version in
   631  	@# $(C_DEPS_DIR)/protobuf-rebuild. See above for rationale.
   632  	cd $(PROTOBUF_DIR) && cmake $(xcmake-flags) -Dprotobuf_BUILD_TESTS=OFF $(PROTOBUF_SRC_DIR)/cmake \
   633  	  -DCMAKE_BUILD_TYPE=Release
   634  
   635  ifneq ($(PROTOC_DIR),$(PROTOBUF_DIR))
   636  $(PROTOC_DIR)/Makefile: $(C_DEPS_DIR)/protobuf-rebuild | bin/.submodules-initialized
   637  	rm -rf $(PROTOC_DIR)
   638  	mkdir -p $(PROTOC_DIR)
   639  	@# NOTE: If you change the CMake flags below, bump the version in
   640  	@# $(C_DEPS_DIR)/protobuf-rebuild. See above for rationale.
   641  	cd $(PROTOC_DIR) && cmake $(CMAKE_FLAGS) -Dprotobuf_BUILD_TESTS=OFF $(PROTOBUF_SRC_DIR)/cmake \
   642  	  -DCMAKE_BUILD_TYPE=Release
   643  endif
   644  
   645  $(ROCKSDB_DIR)/Makefile: sse := $(if $(findstring x86_64,$(TARGET_TRIPLE)),-msse3)
   646  $(ROCKSDB_DIR)/Makefile: $(C_DEPS_DIR)/rocksdb-rebuild | bin/.submodules-initialized $(LIBSNAPPY) $(if $(use-stdmalloc),,$(LIBJEMALLOC))
   647  	rm -rf $(ROCKSDB_DIR)
   648  	mkdir -p $(ROCKSDB_DIR)
   649  	@# NOTE: If you change the CMake flags below, bump the version in
   650  	@# $(C_DEPS_DIR)/rocksdb-rebuild. See above for rationale.
   651  	cd $(ROCKSDB_DIR) && CFLAGS+=" $(sse)" && CXXFLAGS+=" $(sse)" && cmake $(xcmake-flags) $(ROCKSDB_SRC_DIR) \
   652  	  $(if $(findstring release,$(BUILDTYPE)),-DPORTABLE=ON) -DWITH_GFLAGS=OFF \
   653  	  -DSNAPPY_LIBRARIES=$(LIBSNAPPY) -DSNAPPY_INCLUDE_DIR="$(SNAPPY_SRC_DIR);$(SNAPPY_DIR)" -DWITH_SNAPPY=ON \
   654  	  $(if $(use-stdmalloc),,-DJEMALLOC_LIBRARIES=$(LIBJEMALLOC) -DJEMALLOC_INCLUDE_DIR=$(JEMALLOC_DIR)/include -DWITH_JEMALLOC=ON) \
   655  	  -DCMAKE_BUILD_TYPE=$(if $(ENABLE_ROCKSDB_ASSERTIONS),Debug,Release) \
   656  	  -DUSE_RTTI=1 -DFAIL_ON_WARNINGS=0
   657  
   658  $(SNAPPY_DIR)/Makefile: $(C_DEPS_DIR)/snappy-rebuild | bin/.submodules-initialized
   659  	rm -rf $(SNAPPY_DIR)
   660  	mkdir -p $(SNAPPY_DIR)
   661  	@# NOTE: If you change the CMake flags below, bump the version in
   662  	@# $(C_DEPS_DIR)/snappy-rebuild. See above for rationale.
   663  	cd $(SNAPPY_DIR) && cmake $(xcmake-flags) $(SNAPPY_SRC_DIR) \
   664  	  -DCMAKE_BUILD_TYPE=Release
   665  
   666  $(GEOS_DIR)/Makefile: $(C_DEPS_DIR)/geos-rebuild | bin/.submodules-initialized
   667  	rm -rf $(GEOS_DIR)
   668  	mkdir -p $(GEOS_DIR)
   669  	@# NOTE: If you change the CMake flags below, bump the version in
   670  	@# $(C_DEPS_DIR)/geos-rebuild. See above for rationale.
   671  	cd $(GEOS_DIR) && \
   672  	  cmake $(xcmake-flags) $(GEOS_SRC_DIR) -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-fPIC -DCMAKE_CXX_FLAGS=-fPIC
   673  	@# Copy geos/export.h to the capi include directory to avoid needing multiple include
   674  	@# directories.
   675  	mkdir $(GEOS_DIR)/capi/geos
   676  	cp $(GEOS_SRC_DIR)/include/geos/export.h $(GEOS_DIR)/capi/geos
   677  
   678  $(PROJ_DIR)/Makefile: $(C_DEPS_DIR)/proj-rebuild | bin/.submodules-initialized
   679  	rm -rf $(PROJ_DIR)
   680  	mkdir -p $(PROJ_DIR)
   681  	cd $(PROJ_DIR) && cmake  $(xcmake-flags) $(PROJ_SRC_DIR) -DCMAKE_BUILD_TYPE=Release -DBUILD_LIBPROJ_SHARED=OFF
   682  
   683  $(LIBEDIT_SRC_DIR)/configure.ac: | bin/.submodules-initialized
   684  
   685  $(LIBEDIT_SRC_DIR)/configure: $(LIBEDIT_SRC_DIR)/configure.ac
   686  	cd $(LIBEDIT_SRC_DIR) && autoconf
   687  
   688  $(LIBEDIT_DIR)/Makefile: $(C_DEPS_DIR)/libedit-rebuild $(LIBEDIT_SRC_DIR)/configure
   689  	rm -rf $(LIBEDIT_DIR)
   690  	mkdir -p $(LIBEDIT_DIR)
   691  	@# NOTE: If you change the configure flags below, bump the version in
   692  	@# $(C_DEPS_DIR)/libedit-rebuild. See above for rationale.
   693  	cd $(LIBEDIT_DIR) && $(LIBEDIT_SRC_DIR)/configure $(xconfigure-flags) --disable-examples --disable-shared
   694  
   695  # TODO(benesch): make it possible to build libroach without CCL code. Because
   696  # libroach and libroachccl are defined in the same CMake project, CMake requires
   697  # that the CCL code be present even if only the OSS target will be built.
   698  $(LIBROACH_DIR)/Makefile: $(C_DEPS_DIR)/libroach-rebuild | bin/.submodules-initialized bin/.cpp_protobuf_sources bin/.cpp_ccl_protobuf_sources
   699  	rm -rf $(LIBROACH_DIR)
   700  	mkdir -p $(LIBROACH_DIR)
   701  	@# NOTE: If you change the CMake flags below, bump the version in
   702  	@# $(C_DEPS_DIR)/libroach-rebuild. See above for rationale.
   703  	cd $(LIBROACH_DIR) && cmake $(xcmake-flags) $(LIBROACH_SRC_DIR) \
   704  		-DCMAKE_BUILD_TYPE=$(if $(ENABLE_LIBROACH_ASSERTIONS),Debug,Release) \
   705  		-DPROTOBUF_LIB=$(LIBPROTOBUF) -DROCKSDB_LIB=$(LIBROCKSDB) \
   706  		-DJEMALLOC_LIB=$(LIBJEMALLOC) -DSNAPPY_LIB=$(LIBSNAPPY) \
   707  		-DCRYPTOPP_LIB=$(LIBCRYPTOPP)
   708  
   709  # Most of our C and C++ dependencies use Makefiles that are generated by CMake,
   710  # which are rather slow, taking upwards of 500ms to determine that nothing has
   711  # changed. The no-op case is the common case, as C and C++ code is modified
   712  # rarely relative to Go code.
   713  #
   714  # So, for speed, we want to avoid invoking our C and C++ dependencies' build
   715  # systems when nothing has changed. We apply a very coarse heuristic that works
   716  # well in practice: if every file in a given library's source tree is older than
   717  # the compiled library, then the compiled library must be up-to-date.
   718  #
   719  # Normally, you'd accomplish this in Make itself by declaring a prerequisite for
   720  # every file in the library's source tree. For example, you'd have a rule like
   721  # this for protoc:
   722  #
   723  #     $(PROTOC): $(PROTOC_DIR)/Makefile $(shell find c-deps/protobuf)
   724  #       $(MAKE) -C $(PROTOC_DIR) protoc
   725  #
   726  # Note the prerequisite that shells out to the 'find' command. Unfortunately,
   727  # this winds up being as slow as unconditionally invoking the child build
   728  # system! The cost of repeated find invocations, one per command, adds up, plus
   729  # Make needs to stat all of the resulting files, and it seems to do so
   730  # sequentially.
   731  #
   732  # Instead, we unconditionally run the recipe for each C and C++ dependency, via
   733  # .ALWAYS_REBUILD, but condition the execution of the dependency's build system
   734  # on the output of uptodate, a Go binary of our own design. uptodate walks and
   735  # stats the directory tree in parallel, and can make the up-to-date
   736  # determination in under 20ms.
   737  
   738  $(PROTOC): $(PROTOC_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD | $(LIBPROTOBUF)
   739  	@uptodate $@ $(PROTOBUF_SRC_DIR) || $(MAKE) --no-print-directory -C $(PROTOC_DIR) protoc
   740  
   741  $(LIBCRYPTOPP): $(CRYPTOPP_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   742  	@uptodate $@ $(CRYPTOPP_SRC_DIR) || $(MAKE) --no-print-directory -C $(CRYPTOPP_DIR) static
   743  
   744  $(LIBJEMALLOC): $(JEMALLOC_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   745  	@uptodate $@ $(JEMALLOC_SRC_DIR) || $(MAKE) --no-print-directory -C $(JEMALLOC_DIR) build_lib_static
   746  
   747  $(LIBPROTOBUF): $(PROTOBUF_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   748  	@uptodate $@ $(PROTOBUF_SRC_DIR) || $(MAKE) --no-print-directory -C $(PROTOBUF_DIR) libprotobuf
   749  
   750  $(LIBSNAPPY): $(SNAPPY_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   751  	@uptodate $@ $(SNAPPY_SRC_DIR) || $(MAKE) --no-print-directory -C $(SNAPPY_DIR) snappy
   752  
   753  $(LIBGEOS): $(GEOS_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   754  	@uptodate $@ $(GEOS_SRC_DIR) || $(MAKE) --no-print-directory -C $(GEOS_DIR) geos_c
   755  	mkdir -p $(DYN_LIB_DIR)
   756  	ln -sf $(GEOS_DIR)/lib/lib{geos,geos_c}.$(DYN_EXT) $(DYN_LIB_DIR)
   757  
   758  $(LIBPROJ): $(PROJ_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   759  	@uptodate $@ $(PROJ_SRC_DIR) || $(MAKE) --no-print-directory -C $(PROJ_DIR) proj
   760  
   761  $(LIBEDIT): $(LIBEDIT_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   762  	@uptodate $@ $(LIBEDIT_SRC_DIR) || $(MAKE) --no-print-directory -C $(LIBEDIT_DIR)/src
   763  
   764  $(LIBROCKSDB): $(ROCKSDB_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   765  	@uptodate $@ $(ROCKSDB_SRC_DIR) || $(MAKE) --no-print-directory -C $(ROCKSDB_DIR) rocksdb
   766  
   767  libroach-inputs := $(LIBROACH_SRC_DIR) $(ROCKSDB_SRC_DIR)/include $(PROTOBUF_SRC_DIR)/src
   768  
   769  $(LIBROACH): $(LIBROACH_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   770  	@uptodate $@ $(libroach-inputs) || $(MAKE) --no-print-directory -C $(LIBROACH_DIR) roach
   771  
   772  $(LIBROACHCCL): $(LIBROACH_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   773  	@uptodate $@ $(libroach-inputs) || $(MAKE) --no-print-directory -C $(LIBROACH_DIR) roachccl
   774  
   775  $(LIBKRB5): $(KRB5_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
   776  	@uptodate $@ $(KRB5_SRC_DIR)/src || $(MAKE) --no-print-directory -C $(KRB5_DIR)
   777  
   778  # Convenient names for maintainers. Not used by other targets in the Makefile.
   779  .PHONY: protoc libcryptopp libjemalloc libprotobuf libsnappy libgeos libproj librocksdb libroach libroachccl libkrb5
   780  protoc:      $(PROTOC)
   781  libcryptopp: $(LIBCRYPTOPP)
   782  libedit:     $(LIBEDIT)
   783  libjemalloc: $(LIBJEMALLOC)
   784  libprotobuf: $(LIBPROTOBUF)
   785  libsnappy:   $(LIBSNAPPY)
   786  libgeos:     $(LIBGEOS)
   787  libproj:     $(LIBPROJ)
   788  librocksdb:  $(LIBROCKSDB)
   789  libroach:    $(LIBROACH)
   790  libroachccl: $(LIBROACHCCL)
   791  libkrb5:     $(LIBKRB5)
   792  
   793  PHONY: check-libroach
   794  check-libroach: ## Run libroach tests.
   795  check-libroach: $(LIBROACH_DIR)/Makefile $(LIBJEMALLOC) $(LIBPROTOBUF) $(LIBSNAPPY) $(LIBROCKSDB) $(LIBCRYPTOPP)
   796  	@$(MAKE) --no-print-directory -C $(LIBROACH_DIR)
   797  	cd $(LIBROACH_DIR) && ctest -V -R $(TESTS)
   798  
   799  override TAGS += make $(native-tag)
   800  
   801  # Some targets (protobuf) produce different results depending on the sort order;
   802  # set LC_ALL so this is consistent across systems.
   803  export LC_ALL=C
   804  
   805  # defs.mk.sig attempts to capture common cases where defs.mk needs to be
   806  # recomputed, like when compiling for a different platform or using a different
   807  # Go binary. It is not intended to be perfect. Upgrading the compiler toolchain
   808  # in place will go unnoticed, for example. Similar problems exist in all Make-
   809  # based build systems and are not worth solving.
   810  build/defs.mk.sig: sig = $(PATH):$(CURDIR):$(GO):$(GOPATH):$(CC):$(CXX):$(TARGET_TRIPLE):$(BUILDTYPE):$(IGNORE_GOVERS):$(ENABLE_LIBROACH_ASSERTIONS):$(ENABLE_ROCKSDB_ASSERTIONS)
   811  build/defs.mk.sig: .ALWAYS_REBUILD
   812  	@echo '$(sig)' | cmp -s - $@ || echo '$(sig)' > $@
   813  
   814  COCKROACH      := ./cockroach$(SUFFIX)
   815  COCKROACHOSS   := ./cockroachoss$(SUFFIX)
   816  COCKROACHSHORT := ./cockroachshort$(SUFFIX)
   817  
   818  SQLPARSER_TARGETS = \
   819  	pkg/sql/parser/sql.go \
   820  	pkg/sql/parser/helpmap_test.go \
   821  	pkg/sql/parser/help_messages.go \
   822  	pkg/sql/lex/tokens.go \
   823  	pkg/sql/lex/keywords.go \
   824  	pkg/sql/lex/reserved_keywords.go
   825  
   826  PROTOBUF_TARGETS := bin/.go_protobuf_sources bin/.gw_protobuf_sources bin/.cpp_protobuf_sources bin/.cpp_ccl_protobuf_sources
   827  
   828  DOCGEN_TARGETS := bin/.docgen_bnfs bin/.docgen_functions
   829  
   830  EXECGEN_TARGETS = \
   831    pkg/col/coldata/vec.eg.go \
   832    pkg/sql/colexec/and_or_projection.eg.go \
   833    pkg/sql/colexec/any_not_null_agg.eg.go \
   834    pkg/sql/colexec/avg_agg.eg.go \
   835    pkg/sql/colexec/bool_and_or_agg.eg.go \
   836    pkg/sql/colexec/cast.eg.go \
   837    pkg/sql/colexec/const.eg.go \
   838    pkg/sql/colexec/count_agg.eg.go \
   839    pkg/sql/colexec/distinct.eg.go \
   840    pkg/sql/colexec/hashjoiner.eg.go \
   841    pkg/sql/colexec/hashtable_distinct.eg.go \
   842    pkg/sql/colexec/hashtable_full_default.eg.go \
   843    pkg/sql/colexec/hashtable_full_deleting.eg.go \
   844    pkg/sql/colexec/hash_aggregator.eg.go \
   845    pkg/sql/colexec/hash_utils.eg.go \
   846    pkg/sql/colexec/like_ops.eg.go \
   847    pkg/sql/colexec/mergejoinbase.eg.go \
   848    pkg/sql/colexec/mergejoiner_exceptall.eg.go \
   849    pkg/sql/colexec/mergejoiner_fullouter.eg.go \
   850    pkg/sql/colexec/mergejoiner_inner.eg.go \
   851    pkg/sql/colexec/mergejoiner_intersectall.eg.go \
   852    pkg/sql/colexec/mergejoiner_leftanti.eg.go \
   853    pkg/sql/colexec/mergejoiner_leftouter.eg.go \
   854    pkg/sql/colexec/mergejoiner_leftsemi.eg.go \
   855    pkg/sql/colexec/mergejoiner_rightouter.eg.go \
   856    pkg/sql/colexec/min_max_agg.eg.go \
   857    pkg/sql/colexec/ordered_synchronizer.eg.go \
   858    pkg/sql/colexec/overloads_test_utils.eg.go \
   859    pkg/sql/colexec/proj_const_left_ops.eg.go \
   860    pkg/sql/colexec/proj_const_right_ops.eg.go \
   861    pkg/sql/colexec/proj_non_const_ops.eg.go \
   862    pkg/sql/colexec/quicksort.eg.go \
   863    pkg/sql/colexec/rank.eg.go \
   864    pkg/sql/colexec/relative_rank.eg.go \
   865    pkg/sql/colexec/row_number.eg.go \
   866    pkg/sql/colexec/rowstovec.eg.go \
   867    pkg/sql/colexec/selection_ops.eg.go \
   868    pkg/sql/colexec/select_in.eg.go \
   869    pkg/sql/colexec/sort.eg.go \
   870    pkg/sql/colexec/substring.eg.go \
   871    pkg/sql/colexec/sum_agg.eg.go \
   872    pkg/sql/colexec/values_differ.eg.go \
   873    pkg/sql/colexec/vec_comparators.eg.go \
   874    pkg/sql/colexec/window_peer_grouper.eg.go
   875  
   876  .PHONY: remove_obsolete_execgen
   877  remove_obsolete_execgen:
   878  	@obsolete="$(filter-out $(EXECGEN_TARGETS), $(shell find pkg/col/coldata pkg/sql/colexec pkg/sql/exec -name '*.eg.go' 2>/dev/null))"; \
   879  	for file in $${obsolete}; do \
   880  	  echo "Removing obsolete file $${file}..."; \
   881  	  rm -f $${file}; \
   882  	done
   883  
   884  OPTGEN_TARGETS = \
   885  	pkg/sql/opt/memo/expr.og.go \
   886  	pkg/sql/opt/operator.og.go \
   887  	pkg/sql/opt/xform/explorer.og.go \
   888  	pkg/sql/opt/norm/factory.og.go \
   889  	pkg/sql/opt/rule_name.og.go \
   890  	pkg/sql/opt/rule_name_string.go
   891  
   892  go-targets-ccl := \
   893  	$(COCKROACH) $(COCKROACHSHORT) \
   894  	bin/workload \
   895  	go-install \
   896  	bench benchshort \
   897  	check test testshort testslow testrace testraceslow testbuild \
   898  	stress stressrace \
   899  	roachprod-stress roachprod-stressrace \
   900  	generate \
   901  	lint lintshort
   902  
   903  go-targets := $(go-targets-ccl) $(COCKROACHOSS)
   904  
   905  .DEFAULT_GOAL := all
   906  all: build
   907  
   908  .PHONY: c-deps
   909  c-deps: $(C_LIBS_CCL)
   910  
   911  build-mode = build -o $@
   912  
   913  go-install: build-mode = install
   914  
   915  $(COCKROACH) go-install generate: pkg/ui/distccl/bindata.go
   916  
   917  $(COCKROACHOSS): BUILDTARGET = ./pkg/cmd/cockroach-oss
   918  $(COCKROACHOSS): $(C_LIBS_OSS) pkg/ui/distoss/bindata.go
   919  
   920  $(COCKROACHSHORT): BUILDTARGET = ./pkg/cmd/cockroach-short
   921  
   922  $(go-targets-ccl): $(C_LIBS_CCL)
   923  
   924  BUILDINFO = .buildinfo/tag .buildinfo/rev
   925  BUILD_TAGGED_RELEASE =
   926  
   927  ## Override for .buildinfo/tag
   928  BUILDINFO_TAG :=
   929  
   930  $(go-targets): bin/.bootstrap $(BUILDINFO) $(CGO_FLAGS_FILES) $(PROTOBUF_TARGETS)
   931  $(go-targets): $(SQLPARSER_TARGETS) $(EXECGEN_TARGETS) $(OPTGEN_TARGETS)
   932  $(go-targets): override LINKFLAGS += \
   933  	-X "github.com/cockroachdb/cockroach/pkg/build.tag=$(if $(BUILDINFO_TAG),$(BUILDINFO_TAG),$(shell cat .buildinfo/tag))" \
   934  	-X "github.com/cockroachdb/cockroach/pkg/build.rev=$(shell cat .buildinfo/rev)" \
   935  	-X "github.com/cockroachdb/cockroach/pkg/build.cgoTargetTriple=$(TARGET_TRIPLE)" \
   936  	$(if $(BUILDCHANNEL),-X "github.com/cockroachdb/cockroach/pkg/build.channel=$(BUILDCHANNEL)") \
   937  	$(if $(BUILD_TAGGED_RELEASE),-X "github.com/cockroachdb/cockroach/pkg/util/log.crashReportEnv=$(if $(BUILDINFO_TAG),$(BUILDINFO_TAG),$(shell cat .buildinfo/tag))")
   938  
   939  # The build.utcTime format must remain in sync with TimeFormat in
   940  # pkg/build/info.go. It is not installed in tests to avoid busting the cache on
   941  # every rebuild.
   942  $(COCKROACH) $(COCKROACHOSS) $(COCKROACHSHORT) go-install: override LINKFLAGS += \
   943  	-X "github.com/cockroachdb/cockroach/pkg/build.utcTime=$(shell date -u '+%Y/%m/%d %H:%M:%S')"
   944  
   945  SETTINGS_DOC_PAGE := docs/generated/settings/settings.html
   946  
   947  # Note: We pass `-v` to `go build` and `go test -i` so that warnings
   948  # from the linker aren't suppressed. The usage of `-v` also shows when
   949  # dependencies are rebuilt which is useful when switching between
   950  # normal and race test builds.
   951  .PHONY: go-install
   952  $(COCKROACH) $(COCKROACHOSS) $(COCKROACHSHORT) go-install:
   953  	 $(xgo) $(build-mode) -v $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' $(BUILDTARGET)
   954  
   955  # The build targets, in addition to producing a Cockroach binary, silently
   956  # regenerate SQL diagram BNFs and some other doc pages. Generating these docs
   957  # doesn't really belong in the build target, but when they were only part of the
   958  # generate target it was too easy to forget to regenerate them when necessary
   959  # and burn a CI cycle.
   960  #
   961  # We check these docs into version control in the first place in the hope that
   962  # the diff of the generated docs that shows up in Reviewable, 'git diff', etc.
   963  # makes it obvious when a commit has broken the docs. For example, it's very
   964  # easy for changes to the SQL parser to result in unintelligible railroad
   965  # diagrams. When the generated files are not checked in, the breakage goes
   966  # unnoticed until the docs team comes along, potentially months later. Much
   967  # better to make the developer who introduces the breakage fix the breakage.
   968  .PHONY: build buildoss buildshort
   969  build: ## Build the CockroachDB binary.
   970  buildoss: ## Build the CockroachDB binary without any CCL-licensed code.
   971  buildshort: ## Build the CockroachDB binary without the admin UI.
   972  build: $(COCKROACH)
   973  buildoss: $(COCKROACHOSS)
   974  buildshort: $(COCKROACHSHORT)
   975  build buildoss buildshort: $(DOCGEN_TARGETS)
   976  build buildshort: $(if $(is-cross-compile),,$(SETTINGS_DOC_PAGE))
   977  
   978  # For historical reasons, symlink cockroach to cockroachshort.
   979  # TODO(benesch): see if it would break anyone's workflow to remove this.
   980  buildshort:
   981  	ln -sf $(COCKROACHSHORT) $(COCKROACH)
   982  
   983  .PHONY: install
   984  install: ## Install the CockroachDB binary.
   985  install: $(COCKROACH)
   986  	$(INSTALL) -d -m 755 $(DESTDIR)$(bindir)
   987  	$(INSTALL) -m 755 $(COCKROACH) $(DESTDIR)$(bindir)/cockroach
   988  
   989  .PHONY: start
   990  start: $(COCKROACH)
   991  start:
   992  	$(COCKROACH) start $(STARTFLAGS)
   993  
   994  # Build, but do not run the tests.
   995  # PKG is expanded and all packages are built and moved to their directory.
   996  .PHONY: testbuild
   997  testbuild:
   998  	$(xgo) list -tags '$(TAGS)' -f \
   999  	'$(xgo) test -v $(GOFLAGS) -tags '\''$(TAGS)'\'' -ldflags '\''$(LINKFLAGS)'\'' -c {{.ImportPath}} -o {{.Dir}}/{{.Name}}.test' $(PKG) | \
  1000  	$(SHELL)
  1001  
  1002  testshort: override TESTFLAGS += -short
  1003  
  1004  testrace: ## Run tests with the Go race detector enabled.
  1005  testrace stressrace roachprod-stressrace: override GOFLAGS += -race
  1006  testrace stressrace roachprod-stressrace: export GORACE := halt_on_error=1
  1007  testrace stressrace roachprod-stressrace: TESTTIMEOUT := $(RACETIMEOUT)
  1008  
  1009  # Directory scans in the builder image are excruciatingly slow when running
  1010  # Docker for Mac, so we filter out the 20k+ UI dependencies that are
  1011  # guaranteed to be irrelevant to save nearly 10s on every Make invocation.
  1012  FIND_RELEVANT := find ./pkg -name node_modules -prune -o
  1013  
  1014  bench: ## Run benchmarks.
  1015  bench benchshort: TESTS := -
  1016  bench benchshort: BENCHES := .
  1017  bench benchshort: TESTTIMEOUT := $(BENCHTIMEOUT)
  1018  
  1019  # -benchtime=1ns runs one iteration of each benchmark. The -short flag is set so
  1020  # that longer running benchmarks can skip themselves.
  1021  benchshort: override TESTFLAGS += -benchtime=1ns -short
  1022  
  1023  .PHONY: check test testshort testrace testlogic testbaselogic testccllogic testoptlogic bench benchshort
  1024  test: ## Run tests.
  1025  check test testshort testrace bench benchshort:
  1026  	$(xgo) test $(GOTESTFLAGS) $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' -run "$(TESTS)" $(if $(BENCHES),-bench "$(BENCHES)") -timeout $(TESTTIMEOUT) $(PKG) $(TESTFLAGS)
  1027  
  1028  .PHONY: stress stressrace
  1029  stress: ## Run tests under stress.
  1030  stressrace: ## Run tests under stress with the race detector enabled.
  1031  stress stressrace:
  1032  	$(xgo) test $(GOTESTFLAGS) $(GOFLAGS) -exec 'stress $(STRESSFLAGS)' -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' -run "$(TESTS)" -timeout 0 $(PKG) $(filter-out -v,$(TESTFLAGS)) -v -args -test.timeout $(TESTTIMEOUT)
  1033  
  1034  .PHONY: roachprod-stress roachprod-stressrace
  1035  roachprod-stress roachprod-stressrace: bin/roachprod-stress
  1036  	# The bootstrap target creates, among other things, ./bin/stress.
  1037  	@if [ -z "$(CLUSTER)" ]; then \
  1038  		echo "ERROR: missing or empty CLUSTER; create one via:"; \
  1039  		echo "roachprod create \$$USER-stress -n 20 --gce-machine-type=n1-standard-8 --local-ssd=false"; \
  1040  		exit 1; \
  1041  	fi
  1042  	build/builder.sh make bin/.bootstrap
  1043  	build/builder.sh mkrelease amd64-linux-gnu test GOFLAGS="$(GOFLAGS)" TESTFLAGS="-v -c -o $(notdir $(patsubst %/,%,$(PKG))).test" PKG=$(PKG)
  1044  	bin/roachprod-stress $(CLUSTER) $(patsubst github.com/cockroachdb/cockroach/%,./%,$(PKG)) $(STRESSFLAGS) -- \
  1045  	  -test.run "$(TESTS)" $(filter-out -v,$(TESTFLAGS)) -test.v -test.timeout $(TESTTIMEOUT); \
  1046  
  1047  testlogic: testbaselogic testoptlogic testccllogic
  1048  
  1049  testbaselogic: ## Run SQL Logic Tests.
  1050  testbaselogic: bin/logictest
  1051  
  1052  testccllogic: ## Run SQL CCL Logic Tests.
  1053  testccllogic: bin/logictestccl
  1054  
  1055  testoptlogic: ## Run SQL Logic Tests from opt package.
  1056  testoptlogic: bin/logictestopt
  1057  
  1058  logic-test-selector := $(if $(TESTCONFIG),^$(TESTCONFIG)$$)/$(if $(FILES),^$(subst $(space),$$|^,$(FILES))$$)/$(SUBTESTS)
  1059  testbaselogic: TESTS := TestLogic/$(logic-test-selector)
  1060  testccllogic: TESTS := TestCCLLogic/$(logic-test-selector)
  1061  testoptlogic: TESTS := TestExecBuild/$(logic-test-selector)
  1062  
  1063  # Note: we specify -config here in addition to the filter on TESTS
  1064  # above. This is because if we only restrict in TESTS, this will
  1065  # merely cause Go to skip the sub-tests that match the pattern. It
  1066  # does not prevent loading and initializing every default config in
  1067  # turn (including setting up the test clusters, etc.). By specifying
  1068  # -config, the extra initialization overhead is averted.
  1069  testbaselogic testccllogic testoptlogic: TESTFLAGS := -test.v $(if $(FILES),-show-sql) $(if $(TESTCONFIG),-config $(TESTCONFIG))
  1070  testbaselogic testccllogic testoptlogic:
  1071  	cd $($(<F)-package) && $(<F) -test.run "$(TESTS)" -test.timeout $(TESTTIMEOUT) $(TESTFLAGS)
  1072  
  1073  testraceslow: override GOFLAGS += -race
  1074  testraceslow: TESTTIMEOUT := $(RACETIMEOUT)
  1075  
  1076  .PHONY: testslow testraceslow
  1077  testslow testraceslow: override TESTFLAGS += -v
  1078  testslow testraceslow:
  1079  	$(xgo) test $(GOTESTFLAGS) $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' -run "$(TESTS)" $(if $(BENCHES),-bench "$(BENCHES)") -timeout $(TESTTIMEOUT) $(PKG) $(TESTFLAGS) | grep -F ': Test' | sed -E 's/(--- PASS: |\(|\))//g' | awk '{ print $$2, $$1 }' | sort -rn | head -n 10
  1080  
  1081  .PHONY: upload-coverage
  1082  upload-coverage: bin/.bootstrap
  1083  	$(GO) install ./vendor/github.com/wadey/gocovmerge
  1084  	$(GO) install ./vendor/github.com/mattn/goveralls
  1085  	@build/upload-coverage.sh
  1086  
  1087  .PHONY: acceptance
  1088  acceptance: TESTTIMEOUT := $(ACCEPTANCETIMEOUT)
  1089  acceptance: export TESTTIMEOUT := $(TESTTIMEOUT)
  1090  acceptance: ## Run acceptance tests.
  1091  	+@pkg/acceptance/run.sh
  1092  
  1093  .PHONY: compose
  1094  compose: export TESTTIMEOUT := $(TESTTIMEOUT)
  1095  compose: ## Run compose tests.
  1096  	+@pkg/compose/run.sh
  1097  
  1098  .PHONY: dupl
  1099  dupl: bin/.bootstrap
  1100  	$(FIND_RELEVANT) \
  1101  	       -name '*.go'             \
  1102  	       -not -name '*.pb.go'     \
  1103  	       -not -name '*.pb.gw.go'  \
  1104  	       -not -name 'bindata.go' \
  1105  	       -not -name '*_string.go' \
  1106  	       -not -name 'sql.go'      \
  1107  	       -not -name 'irgen.go'    \
  1108  	       -not -name '*.ir.go'     \
  1109  	| dupl -files $(DUPLFLAGS)
  1110  
  1111  .PHONY: generate
  1112  generate: ## Regenerate generated code.
  1113  generate: protobuf $(DOCGEN_TARGETS) $(EXECGEN_TARGETS) $(OPTGEN_TARGETS) $(SQLPARSER_TARGETS) $(SETTINGS_DOC_PAGE) bin/langgen bin/terraformgen
  1114  	$(GO) generate $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' $(PKG)
  1115  
  1116  lint lintshort: TESTTIMEOUT := $(LINTTIMEOUT)
  1117  
  1118  .PHONY: lint
  1119  lint: override TAGS += lint
  1120  lint: ## Run all style checkers and linters.
  1121  lint: bin/returncheck bin/roachvet bin/optfmt
  1122  	@if [ -t 1 ]; then echo '$(yellow)NOTE: `make lint` is very slow! Perhaps `make lintshort`?$(term-reset)'; fi
  1123  	@# Run 'go build -i' to ensure we have compiled object files available for all
  1124  	@# packages. In Go 1.10, only 'go vet' recompiles on demand. For details:
  1125  	@# https://groups.google.com/forum/#!msg/golang-dev/qfa3mHN4ZPA/X2UzjNV1BAAJ.
  1126  	$(xgo) build -i -v $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' $(PKG)
  1127  	$(xgo) test $(GOTESTFLAGS) ./pkg/testutils/lint -v $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' -timeout $(TESTTIMEOUT) -run 'Lint/$(TESTS)'
  1128  
  1129  .PHONY: lintshort
  1130  lintshort: override TAGS += lint
  1131  lintshort: ## Run a fast subset of the style checkers and linters.
  1132  lintshort: bin/roachvet bin/optfmt
  1133  	$(xgo) test $(GOTESTFLAGS) ./pkg/testutils/lint -v $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' -short -timeout $(TESTTIMEOUT) -run 'TestLint/$(TESTS)'
  1134  
  1135  .PHONY: protobuf
  1136  protobuf: $(PROTOBUF_TARGETS)
  1137  protobuf: ## Regenerate generated code for protobuf definitions.
  1138  
  1139  # pre-push locally runs most of the checks CI will run. Notably, it doesn't run
  1140  # the acceptance tests.
  1141  .PHONY: pre-push
  1142  pre-push: ## Run generate, lint, and test.
  1143  pre-push: generate lint test ui-lint ui-test
  1144  	! git status --porcelain | read || (git status; git --no-pager diff -a 1>&2; exit 1)
  1145  
  1146  # archive builds a source tarball out of this repository. Files in the special
  1147  # directory build/archive/contents are inserted directly into $(ARCHIVE_BASE).
  1148  # All other files in the repository are inserted into the archive with prefix
  1149  # $(ARCHIVE_BASE)/src/github.com/cockroachdb/cockroach to allow the extracted
  1150  # archive to serve directly as a GOPATH root.
  1151  .PHONY: archive
  1152  archive: ## Build a source tarball from this repository.
  1153  archive: $(ARCHIVE)
  1154  
  1155  $(ARCHIVE): $(ARCHIVE).tmp
  1156  	gzip -c $< > $@
  1157  
  1158  # ARCHIVE_EXTRAS are hard-to-generate files and their prerequisites that are
  1159  # pre-generated and distributed in source archives to minimize the number of
  1160  # dependencies required for end-users to build from source.
  1161  ARCHIVE_EXTRAS = \
  1162  	$(BUILDINFO) \
  1163  	$(SQLPARSER_TARGETS) \
  1164  	$(EXECGEN_TARGETS) \
  1165  	$(OPTGEN_TARGETS) \
  1166  	pkg/ui/distccl/bindata.go pkg/ui/distoss/bindata.go
  1167  
  1168  # TODO(benesch): Make this recipe use `git ls-files --recurse-submodules`
  1169  # instead of scripts/ls-files.sh once Git v2.11 is widely deployed.
  1170  .INTERMEDIATE: $(ARCHIVE).tmp
  1171  $(ARCHIVE).tmp: ARCHIVE_BASE = cockroach-$(if $(BUILDINFO_TAG),$(BUILDINFO_TAG),$(shell cat .buildinfo/tag))
  1172  $(ARCHIVE).tmp: $(ARCHIVE_EXTRAS)
  1173  	echo "$(if $(BUILDINFO_TAG),$(BUILDINFO_TAG),$(shell cat .buildinfo/tag))" > .buildinfo/tag
  1174  	scripts/ls-files.sh | $(TAR) -cf $@ -T - $(TAR_XFORM_FLAG),^,$(ARCHIVE_BASE)/src/github.com/cockroachdb/cockroach/, $^
  1175  	(cd build/archive/contents && $(TAR) -rf ../../../$@ $(TAR_XFORM_FLAG),^,$(ARCHIVE_BASE)/, *)
  1176  
  1177  .buildinfo:
  1178  	@mkdir -p $@
  1179  
  1180  # Do not use plumbing commands, like git diff-index, in this target. Our build
  1181  # process modifies files quickly enough that plumbing commands report false
  1182  # positives on filesystems with only one second of resolution as a performance
  1183  # optimization. Porcelain commands, like git diff, exist to detect and remove
  1184  # these false positives.
  1185  #
  1186  # For details, see the "Possible timestamp problems with diff-files?" thread on
  1187  # the Git mailing list (http://marc.info/?l=git&m=131687596307197).
  1188  .buildinfo/tag: | .buildinfo
  1189  	@{ git describe --tags --dirty --match=v[0-9]* 2> /dev/null || git rev-parse --short HEAD; } | tr -d \\n > $@
  1190  
  1191  .buildinfo/rev: | .buildinfo
  1192  	@git rev-parse HEAD > $@
  1193  
  1194  ifneq ($(GIT_DIR),)
  1195  # If we're in a Git checkout, we update the buildinfo information on every build
  1196  # to keep it up-to-date.
  1197  .buildinfo/tag: .ALWAYS_REBUILD
  1198  .buildinfo/rev: .ALWAYS_REBUILD
  1199  endif
  1200  
  1201  CPP_PROTO_ROOT := $(LIBROACH_SRC_DIR)/protos
  1202  CPP_PROTO_CCL_ROOT := $(LIBROACH_SRC_DIR)/protosccl
  1203  
  1204  GOGO_PROTOBUF_PATH := ./vendor/github.com/gogo/protobuf
  1205  PROTOBUF_PATH  := $(GOGO_PROTOBUF_PATH)/protobuf
  1206  
  1207  GOGOPROTO_PROTO := $(GOGO_PROTOBUF_PATH)/gogoproto/gogo.proto
  1208  
  1209  ERRORS_PATH := ./vendor/github.com/cockroachdb/errors
  1210  ERRORS_PROTO := $(ERRORS_PATH)/errorspb/errors.proto
  1211  
  1212  COREOS_PATH := ./vendor/go.etcd.io
  1213  
  1214  GRPC_GATEWAY_GOOGLEAPIS_PACKAGE := github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
  1215  GRPC_GATEWAY_GOOGLEAPIS_PATH := ./vendor/$(GRPC_GATEWAY_GOOGLEAPIS_PACKAGE)
  1216  
  1217  # Map protobuf includes to the Go package containing the generated Go code.
  1218  PROTO_MAPPINGS :=
  1219  PROTO_MAPPINGS := $(PROTO_MAPPINGS)Mgoogle/api/annotations.proto=$(GRPC_GATEWAY_GOOGLEAPIS_PACKAGE)/google/api,
  1220  PROTO_MAPPINGS := $(PROTO_MAPPINGS)Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,
  1221  PROTO_MAPPINGS := $(PROTO_MAPPINGS)Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,
  1222  
  1223  GW_SERVER_PROTOS := ./pkg/server/serverpb/admin.proto ./pkg/server/serverpb/status.proto ./pkg/server/serverpb/authentication.proto
  1224  GW_TS_PROTOS := ./pkg/ts/tspb/timeseries.proto
  1225  
  1226  GW_PROTOS  := $(GW_SERVER_PROTOS) $(GW_TS_PROTOS)
  1227  GW_SOURCES := $(GW_PROTOS:%.proto=%.pb.gw.go)
  1228  
  1229  GO_PROTOS := $(sort $(shell $(FIND_RELEVANT) -type f -name '*.proto' -print))
  1230  GO_SOURCES := $(GO_PROTOS:%.proto=%.pb.go)
  1231  
  1232  PBJS := $(NODE_RUN) pkg/ui/node_modules/.bin/pbjs
  1233  PBTS := $(NODE_RUN) pkg/ui/node_modules/.bin/pbts
  1234  
  1235  # Unlike the protobuf compiler for Go and C++, the protobuf compiler for
  1236  # JavaScript only needs the entrypoint protobufs to be listed. It automatically
  1237  # compiles any protobufs the entrypoints depend upon.
  1238  JS_PROTOS_CCL := $(filter %/ccl/storageccl/engineccl/enginepbccl/stats.proto,$(GO_PROTOS))
  1239  UI_JS_CCL := pkg/ui/ccl/src/js/protos.js
  1240  UI_TS_CCL := pkg/ui/ccl/src/js/protos.d.ts
  1241  UI_PROTOS_CCL := $(UI_JS_CCL) $(UI_TS_CCL)
  1242  
  1243  UI_JS_OSS := pkg/ui/src/js/protos.js
  1244  UI_TS_OSS := pkg/ui/src/js/protos.d.ts
  1245  UI_PROTOS_OSS := $(UI_JS_OSS) $(UI_TS_OSS)
  1246  
  1247  CPP_PROTOS := $(filter %/roachpb/api.proto %/roachpb/metadata.proto %/roachpb/data.proto %/roachpb/internal.proto %/roachpb/errors.proto %util/tracing/recorded_span.proto %/concurrency/lock/locking.proto %/enginepb/mvcc.proto %/enginepb/mvcc3.proto %/enginepb/file_registry.proto %/enginepb/rocksdb.proto %/hlc/legacy_timestamp.proto %/hlc/timestamp.proto %/log/log.proto %/unresolved_addr.proto,$(GO_PROTOS))
  1248  CPP_HEADERS := $(subst ./pkg,$(CPP_PROTO_ROOT),$(CPP_PROTOS:%.proto=%.pb.h))
  1249  CPP_SOURCES := $(subst ./pkg,$(CPP_PROTO_ROOT),$(CPP_PROTOS:%.proto=%.pb.cc))
  1250  
  1251  CPP_PROTOS_CCL := $(filter %/ccl/baseccl/encryption_options.proto %/ccl/storageccl/engineccl/enginepbccl/key_registry.proto %/ccl/storageccl/engineccl/enginepbccl/stats.proto,$(GO_PROTOS))
  1252  CPP_HEADERS_CCL := $(subst ./pkg,$(CPP_PROTO_CCL_ROOT),$(CPP_PROTOS_CCL:%.proto=%.pb.h))
  1253  CPP_SOURCES_CCL := $(subst ./pkg,$(CPP_PROTO_CCL_ROOT),$(CPP_PROTOS_CCL:%.proto=%.pb.cc))
  1254  
  1255  $(GOGOPROTO_PROTO): bin/.submodules-initialized
  1256  $(ERRORS_PROTO): bin/.submodules-initialized
  1257  
  1258  bin/.go_protobuf_sources: $(PROTOC) $(GO_PROTOS) $(GOGOPROTO_PROTO) $(ERRORS_PROTO) bin/.bootstrap bin/protoc-gen-gogoroach
  1259  	$(FIND_RELEVANT) -type f -name '*.pb.go' -exec rm {} +
  1260  	set -e; for dir in $(sort $(dir $(GO_PROTOS))); do \
  1261  	  build/werror.sh $(PROTOC) -Ipkg:./vendor/github.com:$(GOGO_PROTOBUF_PATH):$(PROTOBUF_PATH):$(COREOS_PATH):$(GRPC_GATEWAY_GOOGLEAPIS_PATH):$(ERRORS_PATH) --gogoroach_out=$(PROTO_MAPPINGS),plugins=grpc,import_prefix=github.com/cockroachdb/cockroach/pkg/:./pkg $$dir/*.proto; \
  1262  	done
  1263  	$(SED_INPLACE) -E \
  1264  		-e '/import _ /d' \
  1265  		-e 's!import (fmt|math) "github.com/cockroachdb/cockroach/pkg/(fmt|math)"! !g' \
  1266  		-e 's!github\.com/cockroachdb/cockroach/pkg/(etcd)!go.etcd.io/\1!g' \
  1267  		-e 's!github.com/cockroachdb/cockroach/pkg/((bytes|encoding/binary|errors|fmt|io|math|github\.com|(google\.)?golang\.org)([^a-z]|$$))!\1!g' \
  1268          -e 's!github.com/cockroachdb/cockroach/pkg/errorspb!github.com/cockroachdb/errors/errorspb!g' \
  1269  		-e 's!golang.org/x/net/context!context!g' \
  1270  		$(GO_SOURCES)
  1271  	@# TODO(benesch): Remove the last sed command after https://github.com/grpc/grpc-go/issues/711.
  1272  	gofmt -s -w $(GO_SOURCES)
  1273  	touch $@
  1274  
  1275  bin/.gw_protobuf_sources: $(PROTOC) $(GW_SERVER_PROTOS) $(GW_TS_PROTOS) $(GO_PROTOS) $(GOGOPROTO_PROTO) $(ERRORS_PROTO) bin/.bootstrap
  1276  	$(FIND_RELEVANT) -type f -name '*.pb.gw.go' -exec rm {} +
  1277  	build/werror.sh $(PROTOC) -Ipkg:./vendor/github.com:$(GOGO_PROTOBUF_PATH):$(PROTOBUF_PATH):$(ERRORS_PATH):$(COREOS_PATH):$(GRPC_GATEWAY_GOOGLEAPIS_PATH) --grpc-gateway_out=logtostderr=true,request_context=true:./pkg $(GW_SERVER_PROTOS)
  1278  	build/werror.sh $(PROTOC) -Ipkg:./vendor/github.com:$(GOGO_PROTOBUF_PATH):$(PROTOBUF_PATH):$(ERRORS_PATH):$(COREOS_PATH):$(GRPC_GATEWAY_GOOGLEAPIS_PATH) --grpc-gateway_out=logtostderr=true,request_context=true:./pkg $(GW_TS_PROTOS)
  1279  	@# TODO(benesch): Remove after https://github.com/grpc/grpc-go/issues/711.
  1280  	$(SED_INPLACE) -E 's!golang.org/x/net/context!context!g' $(GW_SOURCES)
  1281  	gofmt -s -w $(GW_SOURCES)
  1282  	@# TODO(jordan,benesch) This can be removed along with the above TODO.
  1283  	goimports -w $(GW_SOURCES)
  1284  	touch $@
  1285  
  1286  bin/.cpp_protobuf_sources: $(PROTOC) $(CPP_PROTOS)
  1287  	rm -rf $(CPP_PROTO_ROOT)
  1288  	mkdir -p $(CPP_PROTO_ROOT)
  1289  	build/werror.sh $(PROTOC) -Ipkg:$(GOGO_PROTOBUF_PATH):$(PROTOBUF_PATH) --cpp_out=lite:$(CPP_PROTO_ROOT) $(CPP_PROTOS)
  1290  	$(SED_INPLACE) -E '/gogoproto/d' $(CPP_HEADERS) $(CPP_SOURCES)
  1291  	touch $@
  1292  
  1293  bin/.cpp_ccl_protobuf_sources: $(PROTOC) $(CPP_PROTOS_CCL)
  1294  	rm -rf $(CPP_PROTO_CCL_ROOT)
  1295  	mkdir -p $(CPP_PROTO_CCL_ROOT)
  1296  	build/werror.sh $(PROTOC) -Ipkg:$(GOGO_PROTOBUF_PATH):$(PROTOBUF_PATH) --cpp_out=lite:$(CPP_PROTO_CCL_ROOT) $(CPP_PROTOS_CCL)
  1297  	$(SED_INPLACE) -E '/gogoproto/d' $(CPP_HEADERS_CCL) $(CPP_SOURCES_CCL)
  1298  	touch $@
  1299  
  1300  # The next two rules must be kept exactly the same except the CCL one depends
  1301  # on one additional proto. They generate the pbjs files from the protobuf
  1302  # definitions, which then act is inputs to the pbts compiler, which creates
  1303  # typescript definitions for the proto files afterwards.
  1304  
  1305  .SECONDARY: $(UI_JS_CCL)
  1306  $(UI_JS_CCL): $(GW_PROTOS) $(GO_PROTOS) $(JS_PROTOS_CCL) pkg/ui/yarn.installed | bin/.submodules-initialized
  1307  	# Add comment recognized by reviewable.
  1308  	echo '// GENERATED FILE DO NOT EDIT' > $@
  1309  	$(PBJS) -t static-module -w es6 --strict-long --keep-case --path pkg --path ./vendor/github.com --path $(GOGO_PROTOBUF_PATH) --path $(ERRORS_PATH) --path $(COREOS_PATH) --path $(GRPC_GATEWAY_GOOGLEAPIS_PATH) $(filter %.proto,$(GW_PROTOS) $(JS_PROTOS_CCL)) >> $@
  1310  
  1311  .SECONDARY: $(UI_JS_OSS)
  1312  $(UI_JS_OSS): $(GW_PROTOS) $(GO_PROTOS) pkg/ui/yarn.installed | bin/.submodules-initialized
  1313  	# Add comment recognized by reviewable.
  1314  	echo '// GENERATED FILE DO NOT EDIT' > $@
  1315  	$(PBJS) -t static-module -w es6 --strict-long --keep-case --path pkg --path ./vendor/github.com --path $(GOGO_PROTOBUF_PATH) --path $(ERRORS_PATH) --path $(COREOS_PATH) --path $(GRPC_GATEWAY_GOOGLEAPIS_PATH) $(filter %.proto,$(GW_PROTOS)) >> $@
  1316  
  1317  # End of PBJS-generated files.
  1318  
  1319  .SECONDARY: $(UI_TS_OSS) $(UI_TS_CCL)
  1320  protos%.d.ts: protos%.js pkg/ui/yarn.installed
  1321  	# Add comment recognized by reviewable.
  1322  	echo '// GENERATED FILE DO NOT EDIT' > $@
  1323  	$(PBTS) $< >> $@
  1324  
  1325  STYLINT            := ./node_modules/.bin/stylint
  1326  TSLINT             := ./node_modules/.bin/tslint
  1327  TSC                := ./node_modules/.bin/tsc
  1328  KARMA              := ./node_modules/.bin/karma
  1329  WEBPACK            := ./node_modules/.bin/webpack
  1330  WEBPACK_DEV_SERVER := ./node_modules/.bin/webpack-dev-server
  1331  WEBPACK_DASHBOARD  := ./opt/node_modules/.bin/webpack-dashboard
  1332  
  1333  .PHONY: ui-generate
  1334  ui-generate: pkg/ui/distccl/bindata.go
  1335  
  1336  .PHONY: ui-fonts
  1337  ui-fonts:
  1338  	pkg/ui/scripts/font-gen
  1339  
  1340  .PHONY: ui-topo
  1341  ui-topo: pkg/ui/yarn.installed
  1342  	pkg/ui/scripts/topo.js
  1343  
  1344  .PHONY: ui-lint
  1345  ui-lint: pkg/ui/yarn.installed $(UI_PROTOS_OSS) $(UI_PROTOS_CCL)
  1346  	$(NODE_RUN) -C pkg/ui $(STYLINT) -c .stylintrc styl
  1347  	$(NODE_RUN) -C pkg/ui $(TSLINT) -c tslint.json -p tsconfig.json
  1348  	@# TODO(benesch): Invoke tslint just once when palantir/tslint#2827 is fixed.
  1349  	$(NODE_RUN) -C pkg/ui $(TSLINT) -c tslint.json *.js
  1350  	$(NODE_RUN) -C pkg/ui $(TSC)
  1351  	@if $(NODE_RUN) -C pkg/ui yarn list | grep phantomjs; then echo ^ forbidden UI dependency >&2; exit 1; fi
  1352  
  1353  # DLLs are Webpack bundles, not Windows shared libraries. See "DLLs for speedy
  1354  # builds" in the UI README for details.
  1355  UI_CCL_DLLS := pkg/ui/dist/protos.ccl.dll.js pkg/ui/dist/vendor.oss.dll.js
  1356  UI_CCL_MANIFESTS := pkg/ui/protos.ccl.manifest.json pkg/ui/vendor.oss.manifest.json
  1357  UI_OSS_DLLS := $(subst .ccl,.oss,$(UI_CCL_DLLS))
  1358  UI_OSS_MANIFESTS := $(subst .ccl,.oss,$(UI_CCL_MANIFESTS))
  1359  
  1360  # (Ab)use pattern rules to teach Make that this one Webpack command produces two
  1361  # files. Normally, Make would run the recipe twice if dist/FOO.js and
  1362  # FOO-manifest.js were both out-of-date. [0]
  1363  #
  1364  # XXX: Ideally we'd scope the dependency on $(UI_PROTOS*) to the appropriate
  1365  # protos DLLs, but Make v3.81 has a bug that causes the dependency to be ignored
  1366  # [1]. We're stuck with this workaround until Apple decides to update the
  1367  # version of Make they ship with macOS or we require a newer version of Make.
  1368  # Such a requirement would need to be strictly enforced, as the way this fails
  1369  # is extremely subtle and doesn't present until the web UI is loaded in the
  1370  # browser.
  1371  #
  1372  # [0]: https://stackoverflow.com/a/3077254/1122351
  1373  # [1]: http://savannah.gnu.org/bugs/?19108
  1374  .SECONDARY: $(UI_CCL_DLLS) $(UI_CCL_MANIFESTS) $(UI_OSS_DLLS) $(UI_OSS_MANIFESTS)
  1375  
  1376  pkg/ui/dist/%.oss.dll.js pkg/ui/%.oss.manifest.json: pkg/ui/webpack.%.js pkg/ui/yarn.installed $(UI_PROTOS_OSS)
  1377  	$(NODE_RUN) -C pkg/ui $(WEBPACK) -p --config webpack.$*.js --env.dist=oss
  1378  
  1379  pkg/ui/dist/%.ccl.dll.js pkg/ui/%.ccl.manifest.json: pkg/ui/webpack.%.js pkg/ui/yarn.installed $(UI_PROTOS_CCL)
  1380  	$(NODE_RUN) -C pkg/ui $(WEBPACK) -p --config webpack.$*.js --env.dist=ccl
  1381  
  1382  .PHONY: ui-test
  1383  ui-test: $(UI_CCL_DLLS) $(UI_CCL_MANIFESTS)
  1384  	$(NODE_RUN) -C pkg/ui $(KARMA) start
  1385  
  1386  .PHONY: ui-test-watch
  1387  ui-test-watch: $(UI_CCL_DLLS) $(UI_CCL_MANIFESTS)
  1388  	$(NODE_RUN) -C pkg/ui $(KARMA) start --no-single-run --auto-watch
  1389  
  1390  .PHONY: ui-test-debug
  1391  ui-test-debug: $(UI_DLLS) $(UI_MANIFESTS)
  1392  	$(NODE_RUN) -C pkg/ui $(KARMA) start --browsers Chrome --no-single-run --debug --auto-watch
  1393  
  1394  pkg/ui/distccl/bindata.go: $(UI_CCL_DLLS) $(UI_CCL_MANIFESTS) $(UI_JS_CCL) $(shell find pkg/ui/ccl -type f)
  1395  pkg/ui/distoss/bindata.go: $(UI_OSS_DLLS) $(UI_OSS_MANIFESTS) $(UI_JS_OSS)
  1396  pkg/ui/dist%/bindata.go: pkg/ui/webpack.app.js $(shell find pkg/ui/src pkg/ui/styl -type f) | bin/.bootstrap
  1397  	find pkg/ui/dist$* -mindepth 1 -not -name dist$*.go -delete
  1398  	set -e; shopt -s extglob; for dll in $(notdir $(filter %.dll.js,$^)); do \
  1399  	  ln -s ../dist/$$dll pkg/ui/dist$*/$${dll/@(.ccl|.oss)}; \
  1400  	done
  1401  	$(NODE_RUN) -C pkg/ui $(WEBPACK) --config webpack.app.js --env.dist=$*
  1402  	go-bindata -pkg dist$* -o $@ -prefix pkg/ui/dist$* pkg/ui/dist$*/...
  1403  	echo 'func init() { ui.Asset = Asset; ui.AssetDir = AssetDir; ui.AssetInfo = AssetInfo }' >> $@
  1404  	gofmt -s -w $@
  1405  	goimports -w $@
  1406  
  1407  pkg/ui/yarn.opt.installed:
  1408  	$(NODE_RUN) -C pkg/ui/opt yarn install
  1409  	touch $@
  1410  
  1411  .PHONY: ui-watch-secure
  1412  ui-watch-secure: override WEBPACK_DEV_SERVER_FLAGS += --https
  1413  ui-watch-secure: export TARGET ?= https://localhost:8080/
  1414  
  1415  .PHONY: ui-watch
  1416  ui-watch: export TARGET ?= http://localhost:8080
  1417  ui-watch ui-watch-secure: PORT := 3000
  1418  ui-watch ui-watch-secure: $(UI_CCL_DLLS) pkg/ui/yarn.opt.installed
  1419  	cd pkg/ui && $(WEBPACK_DASHBOARD) -- $(WEBPACK_DEV_SERVER) --config webpack.app.js --env.dist=ccl --port $(PORT) --mode "development" $(WEBPACK_DEV_SERVER_FLAGS)
  1420  
  1421  .PHONY: ui-clean
  1422  ui-clean: ## Remove build artifacts.
  1423  	find pkg/ui/dist* -mindepth 1 -not -name dist*.go -delete
  1424  	rm -f $(UI_PROTOS_CCL) $(UI_PROTOS_OSS)
  1425  	rm -f pkg/ui/*manifest.json
  1426  
  1427  .PHONY: ui-maintainer-clean
  1428  ui-maintainer-clean: ## Like clean, but also remove installed dependencies
  1429  ui-maintainer-clean: ui-clean
  1430  	rm -rf pkg/ui/node_modules pkg/ui/yarn.installed
  1431  
  1432  .SECONDARY: pkg/sql/parser/gen/sql.go.tmp
  1433  pkg/sql/parser/gen/sql.go.tmp: pkg/sql/parser/gen/sql-gen.y bin/.bootstrap
  1434  	set -euo pipefail; \
  1435  	  ret=$$(cd pkg/sql/parser/gen && goyacc -p sql -o sql.go.tmp sql-gen.y); \
  1436  	  if expr "$$ret" : ".*conflicts" >/dev/null; then \
  1437  	    echo "$$ret"; exit 1; \
  1438  	  fi
  1439  
  1440  # The lex package needs to know about all tokens, because the encode
  1441  # functions and lexing predicates need to know about keywords, and
  1442  # keywords map to the token constants. Therefore, generate the
  1443  # constant tokens in the lex package primarily.
  1444  pkg/sql/lex/tokens.go: pkg/sql/parser/gen/sql.go.tmp
  1445  	(echo "// Code generated by make. DO NOT EDIT."; \
  1446  	 echo "// GENERATED FILE DO NOT EDIT"; \
  1447  	 echo; \
  1448  	 echo "package lex"; \
  1449  	 echo; \
  1450  	 grep '^const [A-Z][_A-Z0-9]* ' $^) > $@.tmp || rm $@.tmp
  1451  	mv -f $@.tmp $@
  1452  
  1453  # The lex package is now the primary source for the token constant
  1454  # definitions. Modify the code generated by goyacc here to refer to
  1455  # the definitions in the lex package.
  1456  pkg/sql/parser/sql.go: pkg/sql/parser/gen/sql.go.tmp | bin/.bootstrap
  1457  	(echo "// Code generated by goyacc. DO NOT EDIT."; \
  1458  	 echo "// GENERATED FILE DO NOT EDIT"; \
  1459  	 cat $^ | \
  1460  	 sed -E 's/^const ([A-Z][_A-Z0-9]*) =.*$$/const \1 = lex.\1/g') > $@.tmp || rm $@.tmp
  1461  	mv -f $@.tmp $@
  1462  	goimports -w $@
  1463  
  1464  # This modifies the grammar to:
  1465  # - improve the types used by the generated parser for non-terminals
  1466  # - expand the help rules.
  1467  #
  1468  # For types:
  1469  # Determine the types that will be migrated to union types by looking
  1470  # at the accessors of sqlSymUnion. The first step in this pipeline
  1471  # prints every return type of a sqlSymUnion accessor on a separate line.
  1472  # The next step regular expression escapes these types. The third
  1473  # (prepending) and the fourth (appending) steps build regular expressions
  1474  # for each of the types and store them in the file. (We make multiple
  1475  # regular expressions because we ran into a limit of characters for a
  1476  # single regex executed by sed.)
  1477  # Then translate the original syntax file, with the types determined
  1478  # above being replaced with the union type in their type declarations.
  1479  .SECONDARY: pkg/sql/parser/gen/sql-gen.y
  1480  pkg/sql/parser/gen/sql-gen.y: pkg/sql/parser/sql.y pkg/sql/parser/replace_help_rules.awk
  1481  	mkdir -p pkg/sql/parser/gen
  1482  	set -euo pipefail; \
  1483  	awk '/func.*sqlSymUnion/ {print $$(NF - 1)}' pkg/sql/parser/sql.y | \
  1484  	sed -e 's/[]\/$$*.^|[]/\\&/g' | \
  1485  	sed -e "s/^/s_(type|token) <(/" | \
  1486  	awk '{print $$0")>_\\1 <union> /* <\\2> */_"}' > pkg/sql/parser/gen/types_regex.tmp; \
  1487  	sed -E -f pkg/sql/parser/gen/types_regex.tmp < pkg/sql/parser/sql.y | \
  1488  	awk -f pkg/sql/parser/replace_help_rules.awk | \
  1489  	sed -Ee 's,//.*$$,,g;s,/[*]([^*]|[*][^/])*[*]/, ,g;s/ +$$//g' > $@.tmp || rm $@.tmp
  1490  	mv -f $@.tmp $@
  1491  	rm pkg/sql/parser/gen/types_regex.tmp
  1492  
  1493  pkg/sql/lex/reserved_keywords.go: pkg/sql/parser/sql.y pkg/sql/parser/reserved_keywords.awk | bin/.bootstrap
  1494  	awk -f pkg/sql/parser/reserved_keywords.awk < $< > $@.tmp || rm $@.tmp
  1495  	mv -f $@.tmp $@
  1496  	gofmt -s -w $@
  1497  
  1498  pkg/sql/lex/keywords.go: pkg/sql/parser/sql.y pkg/sql/lex/all_keywords.go | bin/.bootstrap
  1499  	go run -tags all-keywords pkg/sql/lex/all_keywords.go < $< > $@.tmp || rm $@.tmp
  1500  	mv -f $@.tmp $@
  1501  	gofmt -s -w $@
  1502  
  1503  # This target will print unreserved_keywords which are not actually
  1504  # used in the grammar.
  1505  .PHONY: sqlparser-unused-unreserved-keywords
  1506  sqlparser-unused-unreserved-keywords: pkg/sql/parser/sql.y pkg/sql/parser/unreserved_keywords.awk
  1507  	@for kw in $$(awk -f pkg/sql/parser/unreserved_keywords.awk < $<); do \
  1508  	  if [ $$(grep -c $${kw} $<) -le 2 ]; then \
  1509  	    echo $${kw}; \
  1510  	  fi \
  1511  	done
  1512  
  1513  pkg/sql/parser/helpmap_test.go: pkg/sql/parser/gen/sql-gen.y pkg/sql/parser/help_gen_test.sh | bin/.bootstrap
  1514  	@pkg/sql/parser/help_gen_test.sh < $< >$@.tmp || rm $@.tmp
  1515  	mv -f $@.tmp $@
  1516  	gofmt -s -w $@
  1517  
  1518  pkg/sql/parser/help_messages.go: pkg/sql/parser/sql.y pkg/sql/parser/help.awk | bin/.bootstrap
  1519  	awk -f pkg/sql/parser/help.awk < $< > $@.tmp || rm $@.tmp
  1520  	mv -f $@.tmp $@
  1521  	gofmt -s -w $@
  1522  
  1523  bin/.docgen_bnfs: bin/docgen
  1524  	docgen grammar bnf docs/generated/sql/bnf --quiet
  1525  	touch $@
  1526  
  1527  bin/.docgen_functions: bin/docgen
  1528  	docgen functions docs/generated/sql --quiet
  1529  	touch $@
  1530  
  1531  settings-doc-gen := $(if $(filter buildshort,$(MAKECMDGOALS)),$(COCKROACHSHORT),$(COCKROACH))
  1532  
  1533  $(SETTINGS_DOC_PAGE): $(settings-doc-gen)
  1534  	@$(settings-doc-gen) gen settings-list --format=html > $@
  1535  
  1536  # Produce the dependency list for all the .eg.go files, to make them
  1537  # depend on the right template. We use the -M flag to execgen which
  1538  # produces the dependencies, then include them below.
  1539  .SECONDARY: bin/execgen_out.d
  1540  bin/execgen_out.d: bin/execgen
  1541  	@echo EXECGEN $@; execgen -M $(EXECGEN_TARGETS) >$@.tmp || { rm -f $@.tmp; exit 1; }
  1542  	@mv -f $@.tmp $@
  1543  
  1544  # No need to pull all the world in when a user just wants
  1545  # to know how to invoke `make` or clean up.
  1546  ifneq ($(build-with-dep-files),)
  1547  -include bin/execgen_out.d
  1548  endif
  1549  
  1550  # Generate the colexec files.
  1551  #
  1552  # Note how the dependency work is complete after the cmp/rm/mv
  1553  # dance to write to the output.
  1554  # However, because it does not always change the timestamp of the
  1555  # target file, it is possible to get a situation where the target is
  1556  # older than both bin/execgen and the _tmpl.go file, but does not get
  1557  # changed by this rule. This happens e.g. after a 'git checkout' to a
  1558  # different branch, when the execgen binary and templates do not
  1559  # change across branches.
  1560  #
  1561  # In order to prevent the rule from kicking again in every
  1562  # make invocation, we tweak the timestamp of the output file
  1563  # to be the latest of either bin/execgen or the input template.
  1564  # This makes it just new enough that 'make' will be satisfied
  1565  # that it does not need an update any more.
  1566  #
  1567  # Note that we don't want to ratchet the timestamp all the way
  1568  # to the present, because then it will becomes newer
  1569  # than all the other produced artifacts downstream and force
  1570  # them to rebuild too.
  1571  $(EXECGEN_TARGETS): bin/execgen
  1572  	@echo EXECGEN $@
  1573  	@execgen $@ > $@.tmp || { rm -f $@.tmp; exit 1; }
  1574  	@cmp $@.tmp $@ 2>/dev/null && rm -f $@.tmp || mv -f $@.tmp $@
  1575  	@set -e; \
  1576  	  depfile=$$(execgen -M $@ | cut -d: -f2); \
  1577  	  target_timestamp_file=$${depfile:-bin/execgen}; \
  1578  	  if test bin/execgen -nt $$target_timestamp_file; then \
  1579  	    target_timestamp_file=bin/execgen; \
  1580  	  fi; \
  1581  	  touch -r $$target_timestamp_file $@
  1582  
  1583  # Add a catch-all rule for any non-existent execgen generated
  1584  # files. This prevents build errors when switching between branches
  1585  # that introduce or remove execgen generated files as these files are
  1586  # persisted in bin/%.d dependency lists (e.g. in bin/logictest.d).
  1587  %.eg.go: ;
  1588  
  1589  optgen-defs := pkg/sql/opt/ops/*.opt
  1590  optgen-norm-rules := pkg/sql/opt/norm/rules/*.opt
  1591  optgen-xform-rules := pkg/sql/opt/xform/rules/*.opt
  1592  
  1593  pkg/sql/opt/memo/expr.og.go: $(optgen-defs) bin/optgen
  1594  	optgen -out $@ exprs $(optgen-defs)
  1595  
  1596  pkg/sql/opt/operator.og.go: $(optgen-defs) bin/optgen
  1597  	optgen -out $@ ops $(optgen-defs)
  1598  
  1599  pkg/sql/opt/rule_name.og.go: $(optgen-defs) $(optgen-norm-rules) $(optgen-xform-rules) bin/optgen
  1600  	optgen -out $@ rulenames $(optgen-defs) $(optgen-norm-rules) $(optgen-xform-rules)
  1601  
  1602  pkg/sql/opt/rule_name_string.go: pkg/sql/opt/rule_name.go pkg/sql/opt/rule_name.og.go bin/.bootstrap
  1603  	stringer -output=$@ -type=RuleName $(filter %.go,$^)
  1604  
  1605  pkg/sql/opt/xform/explorer.og.go: $(optgen-defs) $(optgen-xform-rules) bin/optgen
  1606  	optgen -out $@ explorer $(optgen-defs) $(optgen-xform-rules)
  1607  
  1608  pkg/sql/opt/norm/factory.og.go: $(optgen-defs) $(optgen-norm-rules) bin/optgen
  1609  	optgen -out $@ factory $(optgen-defs) $(optgen-norm-rules)
  1610  
  1611  # Format non-generated .cc and .h files in libroach using clang-format.
  1612  .PHONY: c-deps-fmt
  1613  c-deps-fmt:
  1614  	find $(LIBROACH_SRC_DIR) -name '*.cc' -o -name '*.h' | xargs grep -L 'DO NOT EDIT' | xargs clang-format -i
  1615  
  1616  .PHONY: clean-c-deps
  1617  clean-c-deps:
  1618  	rm -rf $(CRYPTOPP_DIR)
  1619  	rm -rf $(JEMALLOC_DIR)
  1620  	rm -rf $(PROTOBUF_DIR)
  1621  	rm -rf $(ROCKSDB_DIR)
  1622  	rm -rf $(SNAPPY_DIR)
  1623  	rm -rf $(GEOS_DIR)
  1624  	rm -rf $(PROJ_DIR)
  1625  	rm -rf $(LIBROACH_DIR)
  1626  	rm -rf $(KRB5_DIR)
  1627  
  1628  .PHONY: unsafe-clean-c-deps
  1629  unsafe-clean-c-deps:
  1630  	git -C $(CRYPTOPP_SRC_DIR) clean -dxf
  1631  	git -C $(JEMALLOC_SRC_DIR) clean -dxf
  1632  	git -C $(PROTOBUF_SRC_DIR) clean -dxf
  1633  	git -C $(ROCKSDB_SRC_DIR)  clean -dxf
  1634  	git -C $(SNAPPY_SRC_DIR)   clean -dxf
  1635  	git -C $(GEOS_SRC_DIR)     clean -dxf
  1636  	git -C $(PROJ_SRC_DIR)     clean -dxf
  1637  	git -C $(LIBROACH_SRC_DIR) clean -dxf
  1638  	git -C $(KRB5_SRC_DIR)     clean -dxf
  1639  
  1640  .PHONY: clean-execgen-files
  1641  clean-execgen-files:
  1642  	find ./pkg/sql/colexec -type f -name '*.eg.go' -exec rm {} +
  1643  	test -d ./pkg/sql/exec && find ./pkg/sql/exec -type f -name '*.eg.go' -exec rm {} + || true
  1644  
  1645  .PHONY: clean
  1646  clean: ## Remove build artifacts.
  1647  clean: clean-c-deps clean-execgen-files
  1648  	rm -rf bin/.go_protobuf_sources bin/.gw_protobuf_sources bin/.cpp_protobuf_sources build/defs.mk*
  1649  	$(GO) clean $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' -i -cache github.com/cockroachdb/cockroach...
  1650  	$(FIND_RELEVANT) -type f \( -name 'zcgo_flags*.go' -o -name '*.test' \) -exec rm {} +
  1651  	for f in cockroach*; do if [ -f "$$f" ]; then rm "$$f"; fi; done
  1652  	rm -rf artifacts bin $(ARCHIVE) pkg/sql/parser/gen
  1653  
  1654  .PHONY: maintainer-clean
  1655  maintainer-clean: ## Like clean, but also remove some auto-generated source code.
  1656  maintainer-clean: clean ui-maintainer-clean
  1657  	rm -f $(SQLPARSER_TARGETS) $(EXECGEN_TARGETS) $(OPTGEN_TARGETS) $(UI_PROTOS_OSS) $(UI_PROTOS_CCL)
  1658  
  1659  .PHONY: unsafe-clean
  1660  unsafe-clean: ## Like maintainer-clean, but also remove ALL untracked/ignored files.
  1661  unsafe-clean: maintainer-clean unsafe-clean-c-deps
  1662  	git clean -dxf
  1663  
  1664  # The following rules automatically generate dependency information for Go
  1665  # binaries. See [0] for details on the approach.
  1666  #
  1667  # [0]: http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
  1668  
  1669  bins = \
  1670    bin/allocsim \
  1671    bin/benchmark \
  1672    bin/cockroach-oss \
  1673    bin/cockroach-short \
  1674    bin/docgen \
  1675    bin/execgen \
  1676    bin/fuzz \
  1677    bin/generate-binary \
  1678    bin/terraformgen \
  1679    bin/github-post \
  1680    bin/github-pull-request-make \
  1681    bin/gossipsim \
  1682    bin/langgen \
  1683    bin/protoc-gen-gogoroach \
  1684    bin/publish-artifacts \
  1685    bin/publish-provisional-artifacts \
  1686    bin/optfmt \
  1687    bin/optgen \
  1688    bin/returncheck \
  1689    bin/roachvet \
  1690    bin/roachprod \
  1691    bin/roachprod-stress \
  1692    bin/roachtest \
  1693    bin/teamcity-trigger \
  1694    bin/uptodate \
  1695    bin/urlcheck \
  1696    bin/workload \
  1697    bin/zerosum
  1698  
  1699  testbins = \
  1700    bin/logictest \
  1701    bin/logictestopt \
  1702    bin/logictestccl
  1703  
  1704  # Mappings for binaries that don't live in pkg/cmd.
  1705  execgen-package = ./pkg/sql/colexec/execgen/cmd/execgen
  1706  langgen-package = ./pkg/sql/opt/optgen/cmd/langgen
  1707  optfmt-package = ./pkg/sql/opt/optgen/cmd/optfmt
  1708  optgen-package = ./pkg/sql/opt/optgen/cmd/optgen
  1709  logictest-package = ./pkg/sql/logictest
  1710  logictestccl-package = ./pkg/ccl/logictestccl
  1711  logictestopt-package = ./pkg/sql/opt/exec/execbuilder
  1712  terraformgen-package = ./pkg/cmd/roachprod/vm/aws/terraformgen
  1713  logictest-bins := bin/logictest bin/logictestopt bin/logictestccl
  1714  
  1715  # Additional dependencies for binaries that depend on generated code.
  1716  #
  1717  # TODO(benesch): Derive this automatically. This is getting out of hand.
  1718  bin/workload bin/docgen bin/execgen bin/roachtest $(logictest-bins): $(SQLPARSER_TARGETS) $(PROTOBUF_TARGETS)
  1719  bin/workload bin/roachtest $(logictest-bins): $(EXECGEN_TARGETS)
  1720  bin/roachtest $(logictest-bins): $(C_LIBS_CCL) $(CGO_FLAGS_FILES) $(OPTGEN_TARGETS)
  1721  
  1722  $(bins): bin/%: bin/%.d | bin/prereqs bin/.submodules-initialized
  1723  	@echo go install -v $*
  1724  	bin/prereqs $(if $($*-package),$($*-package),./pkg/cmd/$*) > $@.d.tmp
  1725  	mv -f $@.d.tmp $@.d
  1726  	@$(GO_INSTALL) -v $(if $($*-package),$($*-package),./pkg/cmd/$*)
  1727  
  1728  $(testbins): bin/%: bin/%.d | bin/prereqs $(SUBMODULES_TARGET)
  1729  	@echo go test -c $($*-package)
  1730  	bin/prereqs -bin-name=$* -test $($*-package) > $@.d.tmp
  1731  	mv -f $@.d.tmp $@.d
  1732  	$(xgo) test $(GOTESTFLAGS) $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LINKFLAGS)' -c -o $@ $($*-package)
  1733  
  1734  bin/prereqs: ./pkg/cmd/prereqs/*.go | bin/.submodules-initialized
  1735  	@echo go install -v ./pkg/cmd/prereqs
  1736  	@$(GO_INSTALL) -v ./pkg/cmd/prereqs
  1737  
  1738  .PHONY: fuzz
  1739  fuzz: ## Run fuzz tests.
  1740  fuzz: bin/fuzz
  1741  	bin/fuzz $(TESTFLAGS) -tests $(TESTS) -timeout $(TESTTIMEOUT) $(PKG)
  1742  
  1743  
  1744  # No need to include all the dependency files if the user is just
  1745  # requesting help or cleanup.
  1746  ifneq ($(build-with-dep-files),)
  1747  .SECONDARY: bin/%.d
  1748  .PRECIOUS: bin/%.d
  1749  bin/%.d: ;
  1750  
  1751  include $(wildcard bin/*.d)
  1752  endif
  1753  
  1754  # Make doesn't expose a list of the variables declared in a given file, so we
  1755  # resort to sed magic. Roughly, this sed command prints VARIABLE in lines of the
  1756  # following forms:
  1757  #
  1758  #     [export] VARIABLE [:+?]=
  1759  #     TARGET-NAME: [export] VARIABLE [:+?]=
  1760  #
  1761  # The additional complexity below handles whitespace and comments.
  1762  #
  1763  # The special comments at the beginning are for Github/Go/Reviewable:
  1764  # https://github.com/golang/go/issues/13560#issuecomment-277804473
  1765  # https://github.com/Reviewable/Reviewable/wiki/FAQ#how-do-i-tell-reviewable-that-a-file-is-generated-and-should-not-be-reviewed
  1766  # Note how the 'prefix' variable is manually appended. This is required by Homebrew.
  1767  .SECONDARY: build/variables.mk
  1768  build/variables.mk: Makefile build/archive/contents/Makefile pkg/ui/Makefile build/defs.mk
  1769  	@echo '# Code generated by Make. DO NOT EDIT.' > $@.tmp
  1770  	@echo '# GENERATED FILE DO NOT EDIT' >> $@.tmp
  1771  	@echo 'define VALID_VARS' >> $@.tmp
  1772  	@sed -nE -e '/^	/d' -e 's/([^#]*)#.*/\1/' \
  1773  	  -e 's/(^|^[^:]+:)[ ]*(export)?[ ]*([[:upper:]_]+)[ ]*[:?+]?=.*/  \3/p' $^ \
  1774  	  | sort -u >> $@.tmp
  1775  	@echo '  prefix' >> $@.tmp
  1776  	@echo 'endef' >> $@.tmp
  1777  	@set -e; \
  1778  	if ! cmp -s $@.tmp $@; then \
  1779  	   mv -f $@.tmp $@; \
  1780  	else rm -f $@.tmp; fi
  1781  
  1782  # Print an error if the user specified any variables on the command line that
  1783  # don't appear in this Makefile. The list of valid variables is automatically
  1784  # rebuilt on the first successful `make` invocation after the Makefile changes.
  1785  #
  1786  # TODO(peter): Figure out how to disallow overriding of variables that
  1787  # are not in the valid list from the environment. The problem is that
  1788  # any environment variable becomes a make variable and environments
  1789  # are dirty. For instance, my includes GREP_COLOR.
  1790  include build/variables.mk
  1791  $(foreach v,$(filter-out $(strip $(VALID_VARS)),$(.VARIABLES)),\
  1792  	$(if $(findstring command line,$(origin $v)),$(error Variable '$v' is not recognized by this Makefile)))