Skip to content

Commit

Permalink
updateing cmake for tidy
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy committed Feb 26, 2024
1 parent 419a0d9 commit e66a949
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 90 deletions.
166 changes: 99 additions & 67 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ project(hcsshim
)

cmake_path(SET CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
find_package(Go 1.17 REQUIRED
find_package(Go 1.18 REQUIRED
COMPONENTS Linter
)

# TODO:
#
# `-trimpath` for building
# common go env variable (GOTOOLCHAIN, GOWORK, GO111MODULE, etc.)
#
# debug vs release build variant: set version, trimpath, symbols, etc...
#
Expand All @@ -37,11 +37,12 @@ if(WIN32)
set(os windows)
endif()

set(go_common_env GOTOOLCHAIN=local GO111MODULE=on GOWORK=off)

# todo: consolidate these and move them under CMAKE_BUILD_DIR
cmake_path(SET BIN_DIR ${CMAKE_SOURCE_DIR}/bin)
cmake_path(SET CMD_BIN ${BIN_DIR}/cmd)
cmake_path(SET TEST_BIN ${BIN_DIR}/test)
cmake_path(SET TOOL_BIN ${BIN_DIR}/tool)
cmake_path(SET TEST_BIN ${BIN_DIR}/test) # testing binaries
cmake_path(SET TOOL_BIN ${BIN_DIR}/tool) # ancillary tools (eg, gotestsum, benchstat, crane)

cmake_path(SET DEP_DIR ${CMAKE_SOURCE_DIR}/deps)
cmake_path(SET OUT_DIR ${CMAKE_SOURCE_DIR}/out)
Expand All @@ -51,7 +52,7 @@ cmake_path(SET TEST_DIR ${CMAKE_SOURCE_DIR}/test)
cmake_path(SET CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BIN_DIR})
cmake_path(SET CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUT_DIR})

file(MAKE_DIRECTORY ${CMD_BIN} ${TEST_BIN} ${TOOL_BIN} ${DEP_DIR} ${OUT_DIR} ${PROTOBUF_DIR})
file(MAKE_DIRECTORY ${TEST_BIN} ${TOOL_BIN} ${DEP_DIR} ${OUT_DIR} ${PROTOBUF_DIR})

if(Go_GOBIN)
cmake_path(SET go_bin Go_GOBIN)
Expand All @@ -70,10 +71,6 @@ foreach (i IN ITEMS
"gotest.tools/gotestsum@latest"
"golang.org/x/perf/cmd/benchstat@latest"
"github.com/google/go-containerregistry/cmd/crane@latest"

"github.com/containerd/containerd/cmd/protoc-gen-gogoctrd"
"github.com/containerd/containerd/cmd/protoc-gen-go-fieldpath"
"github.com/containerd/protobuild"
)
string(REGEX REPLACE [[@.*]] "" url ${i})
cmake_path(GET url FILENAME binary)
Expand All @@ -90,7 +87,7 @@ foreach (i IN ITEMS

# custom targets are alway considered out-of-date, so use a custom command to avoid re-installing
cmake_path(SET "${binary}_loc" ${go_bin}/${binary}${binary_ext})
set(env GOBIN=${go_bin})
set(env GOTOOLCHAIN=local GOBIN=${go_bin})
add_custom_command(OUTPUT "${${binary}_loc}"
COMMAND ${CMAKE_COMMAND} -E env ${env} -- ${Go_EXECUTABLE} install ${i}
COMMENT "installing ${binary} from ${i}"
Expand All @@ -100,10 +97,79 @@ foreach (i IN ITEMS
set_target_properties(${binary} PROPERTIES FOLDER dependencies/go)
endforeach()

##########################################################################################
# go mod tidy and vendor
##########################################################################################

add_custom_target(tidy ALL)
set_target_properties(tidy PROPERTIES FOLDER validation)

function(go_tidy name vendor)
set(flags "-e")

# cmake automatically creates a `${CMAKE_BINARY_DIR}/CMakeFiles/${name}` output for the custom target
set(phony ${CMAKE_BINARY_DIR}/CMakeFiles/go_tidy_${name})
add_custom_target(${name} DEPENDS ${phony})

add_custom_command(OUTPUT ${phony}
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} -- ${Go_EXECUTABLE} mod tidy -v ${flags}
COMMENT "tidying go module ${CMAKE_CURRENT_SOURCE_DIR}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
USES_TERMINAL
)
if (vendor)
add_custom_command(OUTPUT ${phony}
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} -- ${Go_EXECUTABLE} mod vendor ${flags}
APPEND
)
endif()

add_dependencies(tidy ${name})
set_target_properties(${name} PROPERTIES FOLDER validation)
message(STATUS "go mod tidy ${name}")
endfunction()

go_tidy(tidy-root true)

##########################################################################################
# go fmt
##########################################################################################

# TODO: make this a command so we can gofmt the test directory
# running this affects the schema files, so don't add as a dependency for now
add_custom_target(gofmt
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} GOOS=windows -- ${Go_EXECUTABLE} fmt ./...
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} GOOS=linux -- ${Go_EXECUTABLE} fmt ./...
COMMENT "running 'go fmt'"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
USES_TERMINAL
)
set_target_properties(gofmt PROPERTIES FOLDER validation)

##########################################################################################
# go generate
##########################################################################################

add_custom_target(gen
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} -- ${Go_EXECUTABLE} generate -x ./...
DEPENDS tidy-root
COMMENT "running 'go generate'"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
USES_TERMINAL
)
set_target_properties(gen PROPERTIES FOLDER generation)

##########################################################################################
# build binaries
##########################################################################################

add_custom_target(set_version
COMMAND pwsh -NonInteractive -NoProfile -NoLogo -Command "./scripts/Set-VersionInfo.ps1"
DEPENDS tidy-root
COMMENT "run Set-Version script"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

add_custom_target(build
COMMENT "build all project go binaries"
)
Expand All @@ -113,12 +179,11 @@ set_target_properties(build PROPERTIES FOLDER binaries)
function(go_build package os)
cmake_path(GET package FILENAME binary)
cmake_path(SET package ${CMAKE_SOURCE_DIR}/${package})
cmake_path(SET out ${CMD_BIN}/${binary})
cmake_path(SET out ${BIN_DIR}/${binary})
set(flags -trimpath "-ldflags=-s -w")
set(env CGO_ENABLED=0 GO111MODULE=on GOWORK=off GOOS=${os})

if (os STREQUAL windows)
cmake_path(APPEND_STRING out ".exe")
cmake_path(APPEND_STRING out binary_ext)
endif()

set(name ${binary})
Expand All @@ -128,17 +193,20 @@ function(go_build package os)

add_custom_target(${name}
BYPRODUCTS ${out}
COMMAND ${CMAKE_COMMAND} -E env ${env} -- ${Go_EXECUTABLE} build ${flags} -o=${out}
# COMMENT "building go binary ${binary} for ${os}"
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} CGO_ENABLED=0 GOOS=${os} -- ${Go_EXECUTABLE} build ${flags} -o=${out}
DEPENDS set_version tidy-root
COMMENT "building go binary ${binary} for ${os}"
WORKING_DIRECTORY "${package}"
)
add_dependencies(build ${name})
set_target_properties(${name} PROPERTIES FOLDER binaries)

message(STATUS "go binary ${name} - ${out}")
endfunction()

go_build(cmd/containerd-shim-runhcs-v1 windows shim)
go_build(cmd/device-util windows)
go_build(cmd/dmverity-vhd windows)
go_build(cmd/gcs linux)
go_build(cmd/gcstools linux)
go_build(cmd/hooks/wait-paths linux)
Expand All @@ -148,25 +216,30 @@ go_build(cmd/shimdiag windows)
go_build(cmd/tar2ext4 windows)
go_build(cmd/tar2ext4 linux tar2ext4-linux)
go_build(cmd/wclayer windows)

go_build(internal/tools/extendedtask windows)
go_build(internal/tools/grantvmgroupaccess windows vmaccess)
go_build(internal/tools/networkagent windows)
go_build(internal/tools/policyenginesimulator linux)
go_build(internal/tools/securitypolicy linux)
go_build(internal/tools/snp-report linux)
go_build(internal/tools/uvmboot windows)
go_build(internal/tools/zapdir windows)


##########################################################################################
# go tests
##########################################################################################

add_custom_target(test
COMMAND ${CMAKE_COMMAND} -E env GO111MODULE=on GOWORK=off -- ${gotestsum_loc} --format=testname -- -tags=admin "-gcflags=all=-d=checkptr" -v -timeout=30m -race ./...
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} -- ${gotestsum_loc} --format=testname -- -tags=admin "-gcflags=all=-d=checkptr" -v -timeout=30m -race ./...
DEPENDS gotestsum
COMMENT "running go tests"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
USES_TERMINAL
VERBATIM
)
add_custom_target(test_hcn
COMMAND ${CMAKE_COMMAND} -E env GO111MODULE=on GOWORK=off -- ${gotestsum_loc} --format=testname -- -tags=admin -tags=integration "-gcflags=all=-d=checkptr" -v -timeout=30m -race hcn/...
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} -- ${gotestsum_loc} --format=testname -- -tags=admin -tags=integration "-gcflags=all=-d=checkptr" -v -timeout=30m -race hcn/...
DEPENDS gotestsum
COMMENT "running go tests in ./hcn"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Expand All @@ -175,45 +248,6 @@ add_custom_target(test_hcn
)
add_custom_target(test_all DEPENDS test test_hcn)

##########################################################################################
# go mod tidy and vendor
##########################################################################################

add_custom_target(mod ALL)
set_target_properties(mod PROPERTIES FOLDER validation)

function(mod name cmd)
set(env GO111MODULE=on GOWORK=off)
set(flags "-e")

add_custom_target(${name}
COMMAND ${CMAKE_COMMAND} -E env ${env} -- ${Go_EXECUTABLE} mod ${cmd} ${ARGN}
COMMENT "running go mod ${cmd} on ${CMAKE_CURRENT_SOURCE_DIR}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
USES_TERMINAL
)
add_dependencies(mod ${name})
set_target_properties(${name} PROPERTIES FOLDER validation)
message(STATUS "go mod ${name}")
endfunction()

# order is important here (cmake alphabetizes dependencies, so declaration order not guaranteed)
mod(tidy-root tidy -v)
mod(vendor-root vendor)
add_dependencies(vendor-root tidy-root)

##########################################################################################
# go generate
##########################################################################################

add_custom_target(gen
COMMAND ${CMAKE_COMMAND} -E env GO111MODULE=on GOWORK=off -- ${Go_EXECUTABLE} generate -x ./...
DEPENDS mod
COMMENT "running go generate"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_target_properties(gen PROPERTIES FOLDER generation)

##########################################################################################
# lint
##########################################################################################
Expand All @@ -222,13 +256,11 @@ add_custom_target(lint)
set_target_properties(lint PROPERTIES FOLDER validation)

function(lint name os)
set(env GO111MODULE=on GOWORK=off GOOS=${os})
set(flags --timeout=2m --max-issues-per-linter=0 --max-same-issues=0 --modules-download-mode=readonly --verbose --config=${CMAKE_SOURCE_DIR}/.golangci.yml)

add_custom_target(${name}
COMMAND ${CMAKE_COMMAND} -E env ${env} -- ${Go_Linter_EXECUTABLE} run ${flags} ${ARGN}
# COMMAND SET GOWORK=off&& SET GOOS=${os}&& ${Go_Linter_EXECUTABLE} run ${flags} ${ARGN}
DEPENDS mod
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} GOOS=${os} -- ${Go_Linter_EXECUTABLE} run ${flags} ${ARGN}
DEPENDS tidy
COMMENT "linting ${CMAKE_CURRENT_SOURCE_DIR} for ${os}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
USES_TERMINAL
Expand All @@ -239,14 +271,14 @@ function(lint name os)
endfunction()

lint(lint-root windows)
lint(lint-root-linux linux cmd/gcs/... cmd/gcstools/... internal/guest/... internal/tools/... ext4/... pkg/...)
lint(lint-root-linux linux)

##########################################################################################
# protobuf
##########################################################################################

add_custom_target(proto
COMMAND ${CMAKE_COMMAND} -E env GO111MODULE=on GOWORK=off -- powershell.exe -Command "& \"${Go_EXECUTABLE}\" list ./... | Where-Object { \$_ -notlike \"*/vendor/*\" } | ForEach-Object { & \"${protobuild_loc}\" \$_ }"
COMMAND ${CMAKE_COMMAND} -E env ${go_common_env} -- powershell.exe -Command "& \"${Go_EXECUTABLE}\" list ./... | Where-Object { \$_ -notlike \"*/vendor/*\" } | ForEach-Object { & \"${protobuild_loc}\" \$_ }"
DEPENDS protobuild protoc-gen-gogoctrd
COMMENT building protofiles
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Expand All @@ -258,12 +290,12 @@ set_target_properties(proto PROPERTIES FOLDER generation)
# pr validation
##########################################################################################

add_custom_target(validate DEPENDS mod lint gen)
add_custom_target(validate DEPENDS tidy lint gen)
set_target_properties(validate PROPERTIES FOLDER validation)

##########################################################################################
# test
# sub-modules
#
# add test subdirectory after all the custom commands and functions have been defined
# add additional modules after all the custom commands and functions have been defined
##########################################################################################
add_subdirectory(test)
2 changes: 1 addition & 1 deletion cmake/FindGo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ if(Linter IN_LIST _go_comps)
_find_go_fail("Failed to get go version: `${Go_Linter_EXECUTABLE} version` failed with: ${_GO_LINTER_VERSION_RAW}")
endif()

if (_GO_LINTER_VERSION_RAW MATCHES "^golangci-lint has version ([0-9]+)\\.([0-9]+)\\.([0-9]+)")
if (_GO_LINTER_VERSION_RAW MATCHES "^golangci-lint has version [v]?([0-9]+)\\.([0-9]+)\\.([0-9]+)")
_find_go_message(VERBOSE "parsed golangci-lint version string: ${_GO_LINTER_VERSION_RAW}")
set(Go_Linter_VERSION_MAJOR "${CMAKE_MATCH_1}")
set(Go_Linter_VERSION_MINOR "${CMAKE_MATCH_2}")
Expand Down
44 changes: 22 additions & 22 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
cmake_minimum_required(VERSION 3.25)
#
# cmake for test directory, to be imported from root directory
#

##########################################################################################
# go mod tidy and vendor
##########################################################################################

go_tidy(tidy-test false)
add_dependencies(tidy-test tidy-root)

##########################################################################################
# lint
##########################################################################################

lint(lint-test windows)
lint(lint-test-linux linux)

project(hcsshim-tests
DESCRIPTION "hcsshim testing binaries"
LANGUAGES NONE
)
##########################################################################################
# build go test binaries
##########################################################################################
Expand All @@ -18,7 +30,7 @@ function(go_build_test package os race)
cmake_path(APPEND_STRING binary ".test")
cmake_path(SET package ${CMAKE_CURRENT_SOURCE_DIR}/${package})
cmake_path(SET out ${TEST_BIN}/${binary})
set(env GO111MODULE=on GOWORK=off GOOS=${os})
set(env ${go_common_env} GOOS=${os})
set(flags "-tags=functional" "-gcflags=all=-d=checkptr")

if (os STREQUAL windows)
Expand All @@ -28,7 +40,7 @@ function(go_build_test package os race)
set(cgo 0)
if (race)
set(cgo 1)
string(APPEND flags " -race")
list(APPEND flags -race)
endif()
list(APPEND env "CGO_ENABLED=${cgo}")

Expand All @@ -41,29 +53,17 @@ function(go_build_test package os race)
add_custom_target(${name}
BYPRODUCTS ${out}
COMMAND ${CMAKE_COMMAND} -E env ${env} -- ${Go_EXECUTABLE} test ${flags} -c -o=${out}
DEPENDS set_version tidy-test
COMMENT "building go test binary ${binary} for ${os}"
WORKING_DIRECTORY "${package}"
)
add_dependencies(build_test ${name})

message(STATUS "go test binary ${name} - ${out}")
endfunction()

go_build_test(containerd-shim-runhcs-v1 windows false shim.test)
go_build_test(cri-containerd windows false cri.test)
go_build_test(functional windows true func.test)
go_build_test(functional windows false func.test)
go_build_test(runhcs windows false)
go_build_test(gcs linux false)

##########################################################################################
# go mod tidy and vendor
##########################################################################################

mod(tidy-test tidy -v)
add_dependencies(tidy-test tidy-root)

##########################################################################################
# lint
##########################################################################################

lint(lint-test windows)
lint(lint-test-linux linux)

0 comments on commit e66a949

Please sign in to comment.