ccgo 3.7.0

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
//! Benches platform builder
//!
//! Builds and runs Google Benchmark benchmarks 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};

/// Benches platform builder
pub struct BenchesBuilder {}

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

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

    /// Get install directory
    fn install_dir(&self, ctx: &BuildContext) -> PathBuf {
        // CMake installs to cmake_build/{release|debug}/benches/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 benchmarks
    fn cmake_extra_flags(&self, ctx: &BuildContext) -> Result<Vec<String>> {
        let mut flags = vec!["-DBENCHMARK_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 benchmarks using CMake
    fn build_benchmarks(&self, ctx: &BuildContext, incremental: bool) -> Result<()> {
        let build_dir = self.build_dir(ctx);
        let install_dir = self.install_dir(ctx);

        // Clean build directory if not incremental
        if !incremental && 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 benchmarks 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());
        } else {
            build_cmd.arg("--parallel").arg("8");
        }

        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!("Benchmarks built successfully: {}", install_dir.display());
        }

        Ok(())
    }

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

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

        // Search for benchmark 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 _googlebenchmark or _bench suffix
                if file_name.contains("_googlebenchmark") || file_name.contains("_bench") {
                    #[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 benchmark executables found in {}",
                install_dir.display()
            );
        }

        Ok(executables)
    }

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

        // Generate JSON output filename with timestamp
        let now = chrono::Local::now();
        let timestamp = now.format("%Y%m%d_%H%M%S_%6f");
        let system_name = if cfg!(target_os = "macos") {
            "macos"
        } else if cfg!(target_os = "windows") {
            "windows"
        } else {
            "linux"
        };
        let json_output = build_dir.join(format!(
            "benches_on_{}_result_{}.json",
            system_name, timestamp
        ));

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

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

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

            // Add JSON output
            cmd.arg("--benchmark_format=console");
            cmd.arg(format!("--benchmark_out={}", json_output.display()));

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

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

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

            eprintln!("✓ Benchmark completed: {}", exe.display());
        }

        eprintln!("\nBenchmark results: {}", json_output.display());
        Ok(())
    }

    /// Generate IDE project for benchmarks
    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 BenchesBuilder {
    fn platform_name(&self) -> &str {
        "benches"
    }

    fn default_architectures(&self) -> Vec<String> {
        vec![]
    }

    fn validate_prerequisites(&self, _ctx: &BuildContext) -> Result<()> {
        // Check if cmake is available
        if which::which("cmake").is_err() {
            bail!("CMake is required but not found in PATH");
        }
        Ok(())
    }

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

        self.validate_prerequisites(ctx)?;
        self.build_benchmarks(ctx, false)?;

        let duration = start.elapsed();

        // Return a placeholder result
        Ok(BuildResult {
            sdk_archive: self.install_dir(ctx),
            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}/benches
        for subdir in &["release", "debug"] {
            let build_dir = ctx
                .project_root
                .join("cmake_build")
                .join(subdir)
                .join("benches");
            if build_dir.exists() {
                std::fs::remove_dir_all(&build_dir)
                    .with_context(|| format!("Failed to clean {}", build_dir.display()))?;
            }
        }

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

        Ok(())
    }
}

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