/
cmake.go
96 lines (77 loc) · 2.93 KB
/
cmake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package cpp
import (
"bytes"
"fmt"
"path"
"github.com/microsoft/yardl/tooling/internal/formatting"
"github.com/microsoft/yardl/tooling/internal/iocommon"
"github.com/microsoft/yardl/tooling/pkg/dsl"
"github.com/microsoft/yardl/tooling/pkg/packaging"
)
func writeCMakeLists(env *dsl.Environment, options packaging.CppCodegenOptions) error {
b := bytes.Buffer{}
w := formatting.NewIndentedWriter(&b, " ")
objectLibraryName := fmt.Sprintf("%s_generated", formatting.ToSnakeCase(options.PackageInfo.Namespace))
fmt.Fprintf(w, `# This file was generated by the "yardl" tool. DO NOT EDIT.
# To opt out of generating this file, set cpp.generateCMakeLists to false in the _package.yml file.
# To use the object library defined in this file, add the following to your CMakeLists.txt file:
# target_link_libraries(<your target> %s)
# add_subdirectory(<path to this directory>)
set(HOWARD_HINNANT_DATE_MINIMUM_VERSION "3.0.0")
find_package(date ${HOWARD_HINNANT_DATE_MINIMUM_VERSION} REQUIRED)
if(VCPKG_TARGET_TRIPLET)
set(HDF5_CXX_LIBRARIES hdf5::hdf5_cpp-shared)
else()
set(HDF5_CXX_LIBRARIES hdf5::hdf5_cpp)
endif()
set(HDF5_MINIMUM_VERSION "1.10.5")
find_package(HDF5 ${HDF5_MINIMUM_VERSION} REQUIRED COMPONENTS C CXX)
set(XTENSOR_MINIMUM_VERSION "0.21.10")
find_package(xtensor ${XTENSOR_MINIMUM_VERSION} REQUIRED)
set(NLOHMANN_JSON_MINIMUM_VERSION "3.11.1")
find_package(nlohmann_json ${NLOHMANN_JSON_MINIMUM_VERSION} REQUIRED)
`, objectLibraryName)
fmt.Fprintf(w, "add_library(%s OBJECT\n", objectLibraryName)
w.Indented(func() {
if options.InternalGenerateTranslator {
w.WriteStringln("factories.cc")
}
w.WriteStringln("protocols.cc")
w.WriteStringln("types.cc")
w.WriteStringln("ndjson/protocols.cc")
w.WriteStringln("binary/protocols.cc")
w.WriteStringln("hdf5/protocols.cc")
if options.InternalGenerateTranslator {
w.WriteStringln("translator_impl.cc")
}
})
w.WriteString(")\n\n")
fmt.Fprintf(w, "target_link_libraries(%s\n", objectLibraryName)
w.Indented(func() {
w.WriteStringln("PUBLIC ${HDF5_C_LIBRARIES}")
w.WriteStringln("PUBLIC ${HDF5_CXX_LIBRARIES}")
w.WriteStringln("PUBLIC xtensor")
w.WriteStringln("PUBLIC date::date")
w.WriteStringln("PUBLIC nlohmann_json::nlohmann_json")
})
w.WriteString(")\n")
if options.InternalGenerateMocks {
w.WriteStringln("")
mocksObjectLibraryName := fmt.Sprintf("%s_mocks", objectLibraryName)
fmt.Fprintf(w, "add_library(%s OBJECT\n", mocksObjectLibraryName)
w.Indented(func() {
w.WriteStringln("mocks.cc")
})
w.WriteString(")\n\n")
fmt.Fprintf(w, "target_link_libraries(%s\n", mocksObjectLibraryName)
w.Indented(func() {
fmt.Fprintf(w, "PUBLIC %s\n", objectLibraryName)
w.WriteStringln("PUBLIC gtest")
})
w.WriteString(")\n")
}
definitionsPath := path.Join(options.SourcesOutputDir, "CMakeLists.txt")
return iocommon.WriteFileIfNeeded(definitionsPath, b.Bytes(), 0644)
}