ccgo 3.7.2

A high-performance C++ cross-platform build CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Tests platform builder
//!
//! Builds and runs GoogleTest unit tests on the host platform.

use std::path::PathBuf;
use std::process::Command;
use std::time::Instant;

use anyhow::{bail, Context, Result};

use crate::build::{BuildContext, BuildResult, PlatformBuilder};

/// Tests platform builder
pub struct TestsBuilder {}

impl TestsBuilder {
    pub fn new() -> Self {
        Self {}
    }

    /// Get build output directory
    fn build_dir(&self, ctx: &BuildContext) -> PathBuf {
        // Uses cmake_build/{release|debug}/tests/ structure
        let release_subdir = if ctx.options.release {
            "release"
        } else {
            "debug"
        };
        ctx.project_root
            .join("cmake_build")
            .join(release_subdir)
            .join("tests")
    }

    /// Get install directory
    fn install_dir(&self, ctx: &BuildContext) -> PathBuf {
        // Tests are installed to cmake_build/{release|debug}/tests/out/
        self.build_dir(ctx).join("out")
    }

    /// Get CMake generator based on platform
    fn cmake_generator(&self) -> &str {
        if cfg!(target_os = "windows") {
            "Visual Studio 16 2019"
        } else {
            // Use Unix Makefiles for all Unix-like systems (macOS, Linux, etc.)
            "Unix Makefiles"
        }
    }

    /// Get CMake extra flags for tests
    fn cmake_extra_flags(&self, ctx: &BuildContext) -> Result<Vec<String>> {
        let mut flags = vec!["-DGOOGLETEST_SUPPORT=ON".to_string()];

        // Add CCGO_CMAKE_DIR if available
        if let Some(cmake_dir) = ctx.ccgo_cmake_dir() {
            flags.push(format!("-DCCGO_CMAKE_DIR={}", cmake_dir.display()));
        }

        // Add build type for single-config generators (Unix Makefiles, Ninja, etc.)
        // Don't set for multi-config generators (Visual Studio)
        if !cfg!(target_os = "windows") {
            let build_type = if ctx.options.release {
                "Release"
            } else {
                "Debug"
            };
            flags.push(format!("-DCMAKE_BUILD_TYPE={}", build_type));
        }

        // Add macOS specific flags
        if cfg!(target_os = "macos") {
            flags.push("-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=10.9".to_string());
            flags.push("-DENABLE_BITCODE=0".to_string());
        }

        // Add compiler cache if available
        if let Some(cache) = ctx.compiler_cache() {
            for (name, value) in cache.cmake_variables() {
                flags.push(format!("-D{}={}", name, value));
            }
        }

        Ok(flags)
    }

    /// Configure and build tests using CMake
    fn build_tests(&self, ctx: &BuildContext) -> Result<()> {
        let build_dir = self.build_dir(ctx);
        let install_dir = self.install_dir(ctx);

        // Clean build directory
        if build_dir.exists() {
            std::fs::remove_dir_all(&build_dir)
                .with_context(|| format!("Failed to clean {}", build_dir.display()))?;
        }

        // Create build directory
        std::fs::create_dir_all(&build_dir)
            .with_context(|| format!("Failed to create {}", build_dir.display()))?;

        if ctx.options.verbose {
            eprintln!("Building tests in {}...", build_dir.display());
        }

        // Configure with CMake
        // Use -S and -B to explicitly specify source and build directories
        let mut cmake_cmd = Command::new("cmake");
        cmake_cmd
            .arg("-S")
            .arg(&ctx.project_root)
            .arg("-B")
            .arg(&build_dir)
            .arg("-G")
            .arg(self.cmake_generator());

        // Add extra flags
        let extra_flags = self.cmake_extra_flags(ctx)?;
        for flag in &extra_flags {
            cmake_cmd.arg(flag);
        }

        if ctx.options.verbose {
            eprintln!("CMake configure: {:?}", cmake_cmd);
        }

        let status = cmake_cmd
            .status()
            .context("Failed to run CMake configure")?;
        if !status.success() {
            bail!("CMake configure failed");
        }

        // Build
        let mut build_cmd = Command::new("cmake");
        build_cmd
            .arg("--build")
            .arg(".")
            .arg("--target")
            .arg("install")
            .current_dir(&build_dir);

        // Add config for multi-config generators (Windows)
        if cfg!(target_os = "windows") {
            let build_type = if ctx.options.release {
                "Release"
            } else {
                "Debug"
            };
            build_cmd.arg("--config").arg(build_type);
        }

        // Add parallel jobs
        if let Some(jobs) = ctx.options.jobs {
            build_cmd.arg("--parallel").arg(jobs.to_string());
        }

        if ctx.options.verbose {
            eprintln!("CMake build: {:?}", build_cmd);
        }

        let status = build_cmd.status().context("Failed to run CMake build")?;
        if !status.success() {
            bail!("CMake build failed");
        }

        if ctx.options.verbose {
            eprintln!("Tests built successfully: {}", install_dir.display());
        }

        Ok(())
    }

    /// Find test executables in install directory
    fn find_test_executables(&self, ctx: &BuildContext) -> Result<Vec<PathBuf>> {
        let install_dir = self.install_dir(ctx);
        let mut executables = Vec::new();

        if !install_dir.exists() {
            bail!(
                "Test install directory not found: {}",
                install_dir.display()
            );
        }

        // Search for test executables
        for entry in std::fs::read_dir(&install_dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_file() {
                let file_name = path.file_name().unwrap().to_string_lossy();
                // Look for _googletest or _test suffix
                if file_name.contains("_googletest") || file_name.contains("_test") {
                    #[cfg(unix)]
                    {
                        // Check if executable
                        use std::os::unix::fs::PermissionsExt;
                        let metadata = std::fs::metadata(&path)?;
                        if metadata.permissions().mode() & 0o111 != 0 {
                            executables.push(path);
                        }
                    }

                    #[cfg(windows)]
                    {
                        // On Windows, check for .exe extension
                        if file_name.ends_with(".exe") {
                            executables.push(path);
                        }
                    }
                }
            }
        }

        if executables.is_empty() {
            bail!("No test executables found in {}", install_dir.display());
        }

        Ok(executables)
    }

    /// Run test executables with optional filter
    pub fn run_tests(&self, ctx: &BuildContext, filter: Option<&str>) -> Result<()> {
        let executables = self.find_test_executables(ctx)?;
        let build_dir = self.build_dir(ctx);

        // Generate XML output filename with timestamp
        let now = chrono::Local::now();
        let timestamp = now.format("%Y%m%d_%H%M%S_%6f");
        let system_name = std::env::consts::OS;
        let xml_output =
            build_dir.join(format!("tests_on_{}_result_{}.xml", system_name, timestamp));

        for exe in executables {
            eprintln!("\nRunning test: {}", exe.display());

            let mut cmd = Command::new(&exe);

            // Add gtest filter if provided
            if let Some(filter_str) = filter {
                cmd.arg(format!("--gtest_filter={}", filter_str));
            }

            // Add XML output
            cmd.arg(format!("--gtest_output=xml:{}", xml_output.display()));

            if ctx.options.verbose {
                eprintln!("Executing: {:?}", cmd);
            }

            let status = cmd
                .status()
                .with_context(|| format!("Failed to run test executable {}", exe.display()))?;

            if !status.success() {
                bail!("Test {} failed", exe.display());
            }

            eprintln!("✓ Test passed: {}", exe.display());
        }

        eprintln!("\nTest results: {}", xml_output.display());
        Ok(())
    }

    /// Generate IDE project for tests
    pub fn generate_ide_project(&self, ctx: &BuildContext) -> Result<()> {
        let build_dir = self.build_dir(ctx);

        // Clean build directory
        if build_dir.exists() {
            std::fs::remove_dir_all(&build_dir)
                .with_context(|| format!("Failed to clean {}", build_dir.display()))?;
        }

        // Create build directory
        std::fs::create_dir_all(&build_dir)
            .with_context(|| format!("Failed to create {}", build_dir.display()))?;

        eprintln!("Generating IDE project in {}...", build_dir.display());

        // Configure with CMake
        // Use -S and -B to explicitly specify source and build directories
        let mut cmake_cmd = Command::new("cmake");
        cmake_cmd
            .arg("-S")
            .arg(&ctx.project_root)
            .arg("-B")
            .arg(&build_dir)
            .arg("-G")
            .arg(self.cmake_generator());

        // Add extra flags
        let extra_flags = self.cmake_extra_flags(ctx)?;
        for flag in &extra_flags {
            cmake_cmd.arg(flag);
        }

        if ctx.options.verbose {
            eprintln!("CMake configure: {:?}", cmake_cmd);
        }

        let status = cmake_cmd
            .status()
            .context("Failed to run CMake configure")?;
        if !status.success() {
            bail!("CMake configure failed");
        }

        // Find and report project file
        let project_file = if cfg!(target_os = "macos") {
            build_dir.join(format!("{}.xcodeproj", ctx.lib_name()))
        } else if cfg!(target_os = "windows") {
            build_dir.join(format!("{}.sln", ctx.lib_name()))
        } else {
            build_dir.join(format!("{}.workspace", ctx.lib_name()))
        };

        if project_file.exists() {
            eprintln!("\n✓ IDE project generated: {}", project_file.display());

            // Try to open the project
            #[cfg(target_os = "macos")]
            {
                let _ = Command::new("open").arg(&project_file).status();
            }

            #[cfg(target_os = "windows")]
            {
                let _ = Command::new("cmd")
                    .arg("/C")
                    .arg("start")
                    .arg(&project_file)
                    .status();
            }
        } else {
            eprintln!(
                "\n✓ IDE project files generated in: {}",
                build_dir.display()
            );
        }

        Ok(())
    }
}

impl PlatformBuilder for TestsBuilder {
    fn platform_name(&self) -> &str {
        "tests"
    }

    fn default_architectures(&self) -> Vec<String> {
        // Tests build for host architecture
        vec![]
    }

    fn validate_prerequisites(&self, _ctx: &BuildContext) -> Result<()> {
        // Check CMake
        let output = Command::new("cmake")
            .arg("--version")
            .output()
            .context("CMake not found. Please install CMake.")?;

        if !output.status.success() {
            bail!("CMake is not working correctly");
        }

        Ok(())
    }

    fn build(&self, ctx: &BuildContext) -> Result<BuildResult> {
        let start = Instant::now();

        self.validate_prerequisites(ctx)?;
        self.build_tests(ctx)?;

        let duration = start.elapsed();
        let install_dir = self.install_dir(ctx);

        eprintln!(
            "\n✓ Tests built successfully in {:.2}s",
            duration.as_secs_f64()
        );
        eprintln!("  Location: {}", install_dir.display());

        Ok(BuildResult {
            sdk_archive: install_dir,
            symbols_archive: None,
            aar_archive: None,
            duration_secs: duration.as_secs_f64(),
            architectures: vec![],
        })
    }

    fn clean(&self, ctx: &BuildContext) -> Result<()> {
        // Clean new directory structure: cmake_build/{release|debug}/tests
        for subdir in &["release", "debug"] {
            let build_dir = ctx
                .project_root
                .join("cmake_build")
                .join(subdir)
                .join("tests");
            if build_dir.exists() {
                std::fs::remove_dir_all(&build_dir)
                    .with_context(|| format!("Failed to clean {}", build_dir.display()))?;
                eprintln!("Cleaned: {}", build_dir.display());
            }
        }

        // Clean old structure for backwards compatibility: cmake_build/Tests, cmake_build/tests
        for old_dir in &[
            ctx.project_root.join("cmake_build/Tests"),
            ctx.project_root.join("cmake_build/tests"),
        ] {
            if old_dir.exists() {
                std::fs::remove_dir_all(old_dir)
                    .with_context(|| format!("Failed to clean {}", old_dir.display()))?;
                eprintln!("Cleaned: {}", old_dir.display());
            }
        }

        Ok(())
    }
}

impl Default for TestsBuilder {
    fn default() -> Self {
        Self::new()
    }
}