| 127 | } |
| 128 | |
| 129 | func (t Toolchain) generate(toolchain *strings.Builder) error { |
| 130 | writeIfNotEmpty := func(key, value string) { |
| 131 | if value != "" { |
| 132 | fmt.Fprintf(toolchain, "set(%s %q)\n", key, "${TOOLCHAIN}/"+value) |
| 133 | } |
| 134 | } |
| 135 | appendFlags := func(key string, flags []string, indent string) { |
| 136 | for _, item := range flags { |
| 137 | item = strings.TrimSpace(item) |
| 138 | if item == "" { |
| 139 | continue |
| 140 | } |
| 141 | if t.ctx != nil { |
| 142 | if exprVars := t.ctx.ExprVars(); exprVars != nil { |
| 143 | item = exprVars.Expand(item) |
| 144 | } |
| 145 | } |
| 146 | fmt.Fprintf(toolchain, "%sstring(APPEND %s %q)\n", indent, key, " "+item) |
| 147 | } |
| 148 | } |
| 149 | buildType := "" |
| 150 | if t.ctx != nil { |
| 151 | buildType = t.ctx.BuildType() |
| 152 | } |
| 153 | cflags, cxxflags, linkflags := t.effectiveFlags(buildType) |
| 154 | |
| 155 | fmt.Fprintf(toolchain, "\n# ============== Cross-compile target system ============== #\n") |
| 156 | fmt.Fprintf(toolchain, "set(%s %q)\n", "CMAKE_SYSTEM_NAME", t.cmakeSystemName()) |
| 157 | fmt.Fprintf(toolchain, "set(%s %q)\n", "CMAKE_SYSTEM_PROCESSOR", t.SystemProcessor) |
| 158 | |
| 159 | fmt.Fprintf(toolchain, "\n# ============== Cross-compile toolchain ============== #\n") |
| 160 | |
| 161 | switch runtime.GOOS { |
| 162 | case "windows": |
| 163 | if t.Name == "msvc" || t.Name == "clang-cl" || t.Name == "clang" { |
| 164 | fmt.Fprintf(toolchain, "set(%s %q)\n", "TOOLCHAIN", filepath.ToSlash(t.abspath)) |
| 165 | } |
| 166 | |
| 167 | case "linux": |
| 168 | if t.Path == "/usr/bin" { |
| 169 | fmt.Fprintf(toolchain, "set(%s %q)\n", "TOOLCHAIN", "/usr/bin") |
| 170 | } else { |
| 171 | if strings.HasPrefix(t.Url, "file:///") { |
| 172 | fmt.Fprintf(toolchain, "set(%s %q)\n", "TOOLCHAIN", filepath.ToSlash(t.abspath)) |
| 173 | } else { |
| 174 | fmt.Fprintf(toolchain, "set(%s %q)\n", "TOOLCHAIN", "${WORKSPACE_ROOT}/downloads/tools/"+filepath.ToSlash(t.Path)) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | writeIfNotEmpty("CMAKE_C_COMPILER", strings.Split(t.CC, " ")[0]) |
| 180 | writeIfNotEmpty("CMAKE_CXX_COMPILER", strings.Split(t.CXX, " ")[0]) |
| 181 | writeIfNotEmpty("CMAKE_AR", t.AR) |
| 182 | writeIfNotEmpty("CMAKE_LINKER", t.LD) |
| 183 | |
| 184 | // Configure compiler targets are usually required by embed platform, like qnx. |
| 185 | if t.CCompilerTarget != "" { |
| 186 | fmt.Fprintf(toolchain, "set(%s %q)\n", "CMAKE_C_COMPILER_TARGET", t.CCompilerTarget) |