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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//! ELF binary file parsing for library information extraction
//!
//! This module provides functions to extract architecture information
//! from ELF binaries, including Android NDK version and API level
//! from the .note.android.ident section.

/// ELF e_machine values to architecture names
const ELF_MACHINE_MAP: &[(u16, &str)] = &[
    (0x03, "x86"),
    (0x3E, "x86_64"),
    (0x28, "arm"),
    (0xB7, "aarch64"),
    (0x08, "mips"),
    (0xF3, "riscv"),
];

/// Mach-O CPU types to architecture names
#[allow(dead_code)]
const MACHO_CPU_MAP: &[(u32, &str)] = &[
    (0x00000007, "x86"),
    (0x01000007, "x86_64"),
    (0x0000000C, "arm"),
    (0x0100000C, "arm64"),
];

/// PE/COFF Machine types to architecture names
#[allow(dead_code)]
const PE_MACHINE_MAP: &[(u16, &str)] = &[
    (0x014c, "x86"),
    (0x8664, "x64"),
    (0xAA64, "ARM64"),
    (0x01c0, "ARM"),
    (0x01c4, "ARMv7"),
];

/// Android NDK information extracted from ELF .note.android.ident section
#[derive(Debug, Clone)]
pub struct AndroidNdkInfo {
    pub ndk_version: Option<String>,
    pub api_level: Option<u16>,
}

/// Library information extracted from binary files
#[derive(Debug, Clone, Default)]
pub struct LibraryInfo {
    pub arch: Option<String>,
    pub ndk_info: Option<AndroidNdkInfo>,
    /// Maximum p_align across PT_LOAD program headers (ELF shared objects only).
    /// Equivalent to `objdump -p <lib> | grep LOAD` → `align 2**N`.
    /// Common values: 0x1000 (4KB), 0x4000 (16KB), 0x10000 (64KB).
    pub page_align: Option<u64>,
}

impl LibraryInfo {
    /// Format the library info as a display string.
    ///
    /// Returns format like: " [aarch64, 16KB-aligned, NDK r27, API 24]".
    /// Alignment is placed after arch so readers can spot it without scanning
    /// past the platform metadata.
    pub fn to_display_string(&self) -> String {
        let mut parts = Vec::new();

        if let Some(arch) = &self.arch {
            parts.push(arch.clone());
        }

        if let Some(align) = self.page_align {
            parts.push(format_page_align(align));
        }

        if let Some(ndk) = &self.ndk_info {
            if let Some(version) = &ndk.ndk_version {
                parts.push(format!("NDK {}", version));
            }
            if let Some(api) = ndk.api_level {
                parts.push(format!("API {}", api));
            }
        }

        if parts.is_empty() {
            String::new()
        } else {
            format!(" [{}]", parts.join(", "))
        }
    }
}

/// Render a `p_align` value as a human-readable page-alignment label.
///
/// Examples: `4096 → "4KB-aligned"`, `16384 → "16KB-aligned"`,
/// `65536 → "64KB-aligned"`. Non-KB multiples fall back to raw bytes.
pub fn format_page_align(align: u64) -> String {
    if align == 0 {
        return "unaligned".to_string();
    }
    const KB: u64 = 1024;
    const MB: u64 = 1024 * KB;
    if align >= MB && align % MB == 0 {
        format!("{}MB-aligned", align / MB)
    } else if align >= KB && align % KB == 0 {
        format!("{}KB-aligned", align / KB)
    } else {
        format!("{}B-aligned", align)
    }
}

/// Parse ELF file to get architecture
pub fn parse_elf_arch(data: &[u8]) -> Option<String> {
    if data.len() < 20 {
        return None;
    }

    // Check ELF magic
    if &data[..4] != b"\x7fELF" {
        return None;
    }

    // Get endianness: 1 = little, 2 = big
    let is_little_endian = data[5] == 1;

    // e_machine is at offset 18 (2 bytes)
    let e_machine = if is_little_endian {
        u16::from_le_bytes([data[18], data[19]])
    } else {
        u16::from_be_bytes([data[18], data[19]])
    };

    for (machine, name) in ELF_MACHINE_MAP {
        if *machine == e_machine {
            return Some(name.to_string());
        }
    }

    Some(format!("unknown(0x{:X})", e_machine))
}

/// Parse the maximum `p_align` across PT_LOAD program headers.
///
/// This is what `objdump -p <lib> | grep LOAD` reports as `align 2**N`.
/// PT_LOAD (type 1) segments carry the runtime memory layout; their
/// alignment constrains the page size the loader must use. For Android
/// 16KB page-size compliance the value must be at least 0x4000 (16 KiB).
pub fn parse_elf_load_align(data: &[u8]) -> Option<u64> {
    if data.len() < 64 || &data[..4] != b"\x7fELF" {
        return None;
    }

    let elf_class = data[4]; // 1 = 32-bit, 2 = 64-bit
    let is_little_endian = data[5] == 1;

    // e_phoff / e_phentsize / e_phnum live at different offsets per class.
    let (e_phoff, e_phentsize, e_phnum) = if elf_class == 1 {
        if data.len() < 52 {
            return None;
        }
        let e_phoff = read_u32(data, 28, is_little_endian) as usize;
        let e_phentsize = read_u16(data, 42, is_little_endian) as usize;
        let e_phnum = read_u16(data, 44, is_little_endian) as usize;
        (e_phoff, e_phentsize, e_phnum)
    } else {
        if data.len() < 64 {
            return None;
        }
        let e_phoff = read_u64(data, 32, is_little_endian) as usize;
        let e_phentsize = read_u16(data, 54, is_little_endian) as usize;
        let e_phnum = read_u16(data, 56, is_little_endian) as usize;
        (e_phoff, e_phentsize, e_phnum)
    };

    if e_phoff == 0 || e_phnum == 0 || e_phentsize == 0 {
        return None;
    }

    const PT_LOAD: u32 = 1;
    let mut max_align: u64 = 0;

    for i in 0..e_phnum {
        let ph_off = e_phoff + i * e_phentsize;
        if ph_off + e_phentsize > data.len() {
            break;
        }

        let p_type = read_u32(data, ph_off, is_little_endian);
        if p_type != PT_LOAD {
            continue;
        }

        // 32-bit program header: p_align is the last u32 (offset 28, size 32).
        // 64-bit program header: p_align is the last u64 (offset 48, size 56).
        let p_align = if elf_class == 1 {
            read_u32(data, ph_off + 28, is_little_endian) as u64
        } else {
            read_u64(data, ph_off + 48, is_little_endian)
        };

        if p_align > max_align {
            max_align = p_align;
        }
    }

    if max_align == 0 {
        None
    } else {
        Some(max_align)
    }
}

/// Parse Android NDK info from ELF .note.android.ident section
pub fn parse_elf_android_ndk_info(data: &[u8]) -> Option<AndroidNdkInfo> {
    if data.len() < 64 || &data[..4] != b"\x7fELF" {
        return None;
    }

    // Get ELF class and endianness
    let elf_class = data[4]; // 1 = 32-bit, 2 = 64-bit
    let is_little_endian = data[5] == 1;

    // Parse ELF header to find section headers
    let (e_shoff, e_shentsize, e_shnum, e_shstrndx) = if elf_class == 1 {
        // 32-bit ELF
        if data.len() < 52 {
            return None;
        }
        let e_shoff = read_u32(data, 32, is_little_endian) as usize;
        let e_shentsize = read_u16(data, 46, is_little_endian) as usize;
        let e_shnum = read_u16(data, 48, is_little_endian) as usize;
        let e_shstrndx = read_u16(data, 50, is_little_endian) as usize;
        (e_shoff, e_shentsize, e_shnum, e_shstrndx)
    } else {
        // 64-bit ELF
        if data.len() < 64 {
            return None;
        }
        let e_shoff = read_u64(data, 40, is_little_endian) as usize;
        let e_shentsize = read_u16(data, 58, is_little_endian) as usize;
        let e_shnum = read_u16(data, 60, is_little_endian) as usize;
        let e_shstrndx = read_u16(data, 62, is_little_endian) as usize;
        (e_shoff, e_shentsize, e_shnum, e_shstrndx)
    };

    if e_shoff == 0 || e_shnum == 0 {
        return None;
    }

    // Read section header string table
    let shstrtab_off = e_shoff + e_shstrndx * e_shentsize;
    if shstrtab_off + e_shentsize > data.len() {
        return None;
    }

    let (strtab_offset, strtab_size) = if elf_class == 1 {
        let offset = read_u32(data, shstrtab_off + 16, is_little_endian) as usize;
        let size = read_u32(data, shstrtab_off + 20, is_little_endian) as usize;
        (offset, size)
    } else {
        let offset = read_u64(data, shstrtab_off + 24, is_little_endian) as usize;
        let size = read_u64(data, shstrtab_off + 32, is_little_endian) as usize;
        (offset, size)
    };

    if strtab_offset + strtab_size > data.len() {
        return None;
    }

    let strtab = &data[strtab_offset..strtab_offset + strtab_size];

    // Find .note.android.ident section
    let target_section = b".note.android.ident";

    for i in 0..e_shnum {
        let sh_off = e_shoff + i * e_shentsize;
        if sh_off + e_shentsize > data.len() {
            continue;
        }

        let sh_name = read_u32(data, sh_off, is_little_endian) as usize;

        let (sec_offset, sec_size) = if elf_class == 1 {
            let offset = read_u32(data, sh_off + 16, is_little_endian) as usize;
            let size = read_u32(data, sh_off + 20, is_little_endian) as usize;
            (offset, size)
        } else {
            let offset = read_u64(data, sh_off + 24, is_little_endian) as usize;
            let size = read_u64(data, sh_off + 32, is_little_endian) as usize;
            (offset, size)
        };

        // Get section name from string table
        if sh_name >= strtab.len() {
            continue;
        }

        let name_end = strtab[sh_name..]
            .iter()
            .position(|&b| b == 0)
            .map(|p| sh_name + p)
            .unwrap_or(strtab.len());
        let section_name = &strtab[sh_name..name_end];

        if section_name == target_section {
            // Found .note.android.ident section
            if sec_offset + sec_size > data.len() {
                return None;
            }

            let note_data = &data[sec_offset..sec_offset + sec_size];
            return parse_android_note(note_data, is_little_endian);
        }
    }

    None
}

/// Parse Android note section content
///
/// Format:
///   uint32_t namesz (8 for "Android\0")
///   uint32_t descsz
///   uint32_t type (1)
///   char name[8] "Android\0"
///   uint16_t api_level
///   uint16_t padding (usually NDK major version in newer NDKs)
///   char ndk_version[] (null-terminated, e.g., "r27")
///   char ndk_build[] (null-terminated, e.g., "12117859")
fn parse_android_note(note_data: &[u8], is_little_endian: bool) -> Option<AndroidNdkInfo> {
    if note_data.len() < 16 {
        return None;
    }

    let namesz = read_u32(note_data, 0, is_little_endian) as usize;
    let descsz = read_u32(note_data, 4, is_little_endian) as usize;
    // let note_type = read_u32(note_data, 8, is_little_endian);

    // Name is aligned to 4 bytes
    let name_aligned = (namesz + 3) & !3;
    let desc_start = 12 + name_aligned;

    if desc_start + descsz > note_data.len() {
        return None;
    }

    // Check name is "Android"
    let name = &note_data[12..12 + namesz];
    let name_str = name.split(|&b| b == 0).next().unwrap_or(&[]);
    if name_str != b"Android" {
        return None;
    }

    let desc = &note_data[desc_start..desc_start + descsz];
    if desc.len() < 4 {
        return None;
    }

    // Parse descriptor
    let api_level = read_u16(desc, 0, is_little_endian);

    // Find NDK version string (starts after api_level + padding)
    let mut ndk_version = None;
    if desc.len() > 4 {
        let remaining = &desc[4..];
        // Parse null-terminated strings
        let mut strings = Vec::new();
        let mut current = Vec::new();

        for &byte in remaining {
            if byte == 0 {
                if !current.is_empty() {
                    if let Ok(s) = String::from_utf8(current.clone()) {
                        strings.push(s);
                    }
                    current.clear();
                }
            } else {
                current.push(byte);
            }
        }
        if !current.is_empty() {
            if let Ok(s) = String::from_utf8(current) {
                strings.push(s);
            }
        }

        // First string starting with 'r' is likely NDK version
        for s in strings {
            if s.starts_with('r') && s.len() <= 10 {
                ndk_version = Some(s);
                break;
            }
        }
    }

    Some(AndroidNdkInfo {
        api_level: Some(api_level),
        ndk_version,
    })
}

/// Get library information from binary data
///
/// Parses the binary to extract architecture info, and for Android .so files,
/// also extracts NDK version and API level.
pub fn get_library_info(data: &[u8], filename: &str, file_path: &str) -> LibraryInfo {
    let mut info = LibraryInfo::default();

    if data.len() < 4 {
        return info;
    }

    let magic = &data[..4];

    // ELF
    if magic == b"\x7fELF" {
        info.arch = parse_elf_arch(data);

        // Page alignment is meaningful for shared objects (.so). Android in
        // particular requires ≥16KB alignment on 16KB-page devices (Google
        // Play enforces this as of Nov 2025).
        if filename.ends_with(".so") {
            info.page_align = parse_elf_load_align(data);
        }

        // Check for Android .so files
        // Match paths containing "android" or "jni/" (AAR format uses jni/ for native libs)
        let path_lower = file_path.to_lowercase();
        if filename.ends_with(".so")
            && (path_lower.contains("android") || path_lower.contains("jni/"))
        {
            info.ndk_info = parse_elf_android_ndk_info(data);
        }
    }
    // Mach-O (various magic values)
    else if matches!(
        magic,
        b"\xfe\xed\xfa\xce"  // 32-bit
            | b"\xfe\xed\xfa\xcf"  // 64-bit
            | b"\xce\xfa\xed\xfe"  // 32-bit reversed
            | b"\xcf\xfa\xed\xfe"  // 64-bit reversed
            | b"\xca\xfe\xba\xbe"  // FAT binary
            | b"\xbe\xba\xfe\xca" // FAT binary reversed
    ) {
        info.arch = parse_macho_arch(data);
    }
    // PE (MZ header)
    else if &magic[..2] == b"MZ" {
        info.arch = parse_pe_arch(data);
    }
    // AR archive
    else if data.len() >= 8 && &data[..8] == b"!<arch>\n" {
        info.arch = parse_ar_member_arch(data);
    }

    info
}

/// Check if a file is a library file that should have info extracted
pub fn is_library_file(filename: &str, file_path: &str) -> bool {
    // Check if in lib, jni, or frameworks directory
    let path_lower = file_path.to_lowercase();
    if !path_lower.contains("lib/")
        && !path_lower.contains("jni/")
        && !path_lower.contains("frameworks/")
    {
        return false;
    }

    // Check extensions
    let lib_extensions = [".a", ".so", ".dylib", ".dll", ".lib"];
    if lib_extensions.iter().any(|ext| filename.ends_with(ext)) {
        return true;
    }

    // macOS framework binary (no extension, inside .framework)
    if file_path.contains(".framework/") && !filename.contains('.') {
        return true;
    }

    false
}

// Helper functions for reading integers from byte slices

fn read_u16(data: &[u8], offset: usize, little_endian: bool) -> u16 {
    if offset + 2 > data.len() {
        return 0;
    }
    if little_endian {
        u16::from_le_bytes([data[offset], data[offset + 1]])
    } else {
        u16::from_be_bytes([data[offset], data[offset + 1]])
    }
}

fn read_u32(data: &[u8], offset: usize, little_endian: bool) -> u32 {
    if offset + 4 > data.len() {
        return 0;
    }
    if little_endian {
        u32::from_le_bytes([
            data[offset],
            data[offset + 1],
            data[offset + 2],
            data[offset + 3],
        ])
    } else {
        u32::from_be_bytes([
            data[offset],
            data[offset + 1],
            data[offset + 2],
            data[offset + 3],
        ])
    }
}

fn read_u64(data: &[u8], offset: usize, little_endian: bool) -> u64 {
    if offset + 8 > data.len() {
        return 0;
    }
    if little_endian {
        u64::from_le_bytes([
            data[offset],
            data[offset + 1],
            data[offset + 2],
            data[offset + 3],
            data[offset + 4],
            data[offset + 5],
            data[offset + 6],
            data[offset + 7],
        ])
    } else {
        u64::from_be_bytes([
            data[offset],
            data[offset + 1],
            data[offset + 2],
            data[offset + 3],
            data[offset + 4],
            data[offset + 5],
            data[offset + 6],
            data[offset + 7],
        ])
    }
}

/// Lookup Mach-O CPU type name from the map
fn lookup_macho_cpu_name(cputype: u32) -> Option<&'static str> {
    MACHO_CPU_MAP
        .iter()
        .find(|(cpu, _)| *cpu == cputype)
        .map(|(_, name)| *name)
}

/// Parse Mach-O file to get architecture
fn parse_macho_arch(data: &[u8]) -> Option<String> {
    if data.len() < 8 {
        return None;
    }

    let magic = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);

    // Check for fat binary (universal)
    if matches!(magic, 0xCAFEBABE | 0xBEBAFECA) {
        return parse_fat_binary_arch(data);
    }

    parse_single_macho_arch(data, magic)
}

/// Parse fat/universal Mach-O binary
fn parse_fat_binary_arch(data: &[u8]) -> Option<String> {
    if data.len() < 8 {
        return None;
    }
    let nfat = u32::from_be_bytes([data[4], data[5], data[6], data[7]]);
    if nfat > 10 {
        // Sanity check
        return None;
    }

    let mut archs = Vec::new();
    for i in 0..nfat as usize {
        let offset = 8 + i * 20;
        if offset + 8 > data.len() {
            break;
        }
        let cputype = u32::from_be_bytes([
            data[offset],
            data[offset + 1],
            data[offset + 2],
            data[offset + 3],
        ]);
        if let Some(name) = lookup_macho_cpu_name(cputype) {
            if !archs.contains(&name.to_string()) {
                archs.push(name.to_string());
            }
        }
    }

    if archs.is_empty() {
        None
    } else {
        Some(archs.join(", "))
    }
}

/// Parse single architecture Mach-O
fn parse_single_macho_arch(data: &[u8], magic: u32) -> Option<String> {
    let is_le = match magic {
        0xFEEDFACE | 0xFEEDFACF => true,  // 32-bit or 64-bit LE
        0xCEFAEDFE | 0xCFFAEDFE => false, // 32-bit or 64-bit BE
        _ => return None,
    };

    if data.len() < 8 {
        return None;
    }

    let cputype = if is_le {
        u32::from_le_bytes([data[4], data[5], data[6], data[7]])
    } else {
        u32::from_be_bytes([data[4], data[5], data[6], data[7]])
    };

    lookup_macho_cpu_name(cputype)
        .map(|s| s.to_string())
        .or_else(|| Some(format!("unknown(0x{:X})", cputype)))
}

/// Parse PE file to get architecture
fn parse_pe_arch(data: &[u8]) -> Option<String> {
    if data.len() < 64 || &data[..2] != b"MZ" {
        return None;
    }

    // Get PE header offset from DOS header at offset 0x3C
    if data.len() < 0x40 {
        return None;
    }
    let pe_offset = u32::from_le_bytes([data[0x3C], data[0x3D], data[0x3E], data[0x3F]]) as usize;

    if pe_offset + 6 > data.len() {
        return None;
    }

    // Check PE signature
    if &data[pe_offset..pe_offset + 4] != b"PE\x00\x00" {
        return None;
    }

    // Machine type is at PE header + 4
    let machine = u16::from_le_bytes([data[pe_offset + 4], data[pe_offset + 5]]);

    for (m, name) in PE_MACHINE_MAP {
        if *m == machine {
            return Some(name.to_string());
        }
    }

    Some(format!("unknown(0x{:X})", machine))
}

/// Parse AR archive to get member architecture
fn parse_ar_member_arch(data: &[u8]) -> Option<String> {
    if data.len() < 68 || &data[..8] != b"!<arch>\n" {
        return None;
    }

    // Skip to first file header (at offset 8)
    // AR format: "!<arch>\n" + 60-byte file header + file content
    let header_size = 60;
    let content_start = 8 + header_size;

    if content_start + 4 > data.len() {
        return None;
    }

    // Try to parse the first member as ELF
    let member_data = &data[content_start..];
    if member_data.len() >= 4 && &member_data[..4] == b"\x7fELF" {
        return parse_elf_arch(member_data);
    }

    // Try Mach-O
    if member_data.len() >= 4 {
        let magic = u32::from_le_bytes([
            member_data[0],
            member_data[1],
            member_data[2],
            member_data[3],
        ]);
        if matches!(
            magic,
            0xFEEDFACE | 0xFEEDFACF | 0xCEFAEDFE | 0xCFFAEDFE | 0xCAFEBABE | 0xBEBAFECA
        ) {
            return parse_macho_arch(member_data);
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_library_file() {
        assert!(is_library_file(
            "libfoo.so",
            "lib/android/shared/arm64-v8a/libfoo.so"
        ));
        assert!(is_library_file("libfoo.so", "jni/arm64-v8a/libfoo.so"));
        assert!(is_library_file(
            "libfoo.a",
            "lib/android/static/arm64-v8a/libfoo.a"
        ));
        assert!(is_library_file("foo.dylib", "lib/macos/shared/foo.dylib"));
        assert!(!is_library_file("foo.txt", "lib/android/foo.txt"));
        assert!(!is_library_file("libfoo.so", "src/libfoo.so"));
    }

    #[test]
    fn test_library_info_display() {
        let info = LibraryInfo {
            arch: Some("aarch64".to_string()),
            ndk_info: Some(AndroidNdkInfo {
                ndk_version: Some("r27".to_string()),
                api_level: Some(24),
            }),
            page_align: Some(16 * 1024),
        };
        assert_eq!(
            info.to_display_string(),
            " [aarch64, 16KB-aligned, NDK r27, API 24]"
        );

        let info2 = LibraryInfo {
            arch: Some("x86_64".to_string()),
            ndk_info: None,
            page_align: Some(4096),
        };
        assert_eq!(info2.to_display_string(), " [x86_64, 4KB-aligned]");

        let info3 = LibraryInfo::default();
        assert_eq!(info3.to_display_string(), "");

        let info4 = LibraryInfo {
            arch: Some("arm".to_string()),
            ndk_info: None,
            page_align: None,
        };
        assert_eq!(info4.to_display_string(), " [arm]");
    }

    #[test]
    fn test_format_page_align() {
        assert_eq!(format_page_align(4096), "4KB-aligned");
        assert_eq!(format_page_align(16 * 1024), "16KB-aligned");
        assert_eq!(format_page_align(64 * 1024), "64KB-aligned");
        assert_eq!(format_page_align(1024 * 1024), "1MB-aligned");
        assert_eq!(format_page_align(512), "512B-aligned");
        assert_eq!(format_page_align(0), "unaligned");
    }
}