xref: /aosp_15_r20/external/libaom/build/cmake/version.pl (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker#!/usr/bin/env perl
2*77c1e3ccSAndroid Build Coastguard Worker##
3*77c1e3ccSAndroid Build Coastguard Worker## Copyright (c) 2016, Alliance for Open Media. All rights reserved.
4*77c1e3ccSAndroid Build Coastguard Worker##
5*77c1e3ccSAndroid Build Coastguard Worker## This source code is subject to the terms of the BSD 2 Clause License and
6*77c1e3ccSAndroid Build Coastguard Worker## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7*77c1e3ccSAndroid Build Coastguard Worker## was not distributed with this source code in the LICENSE file, you can
8*77c1e3ccSAndroid Build Coastguard Worker## obtain it at www.aomedia.org/license/software. If the Alliance for Open
9*77c1e3ccSAndroid Build Coastguard Worker## Media Patent License 1.0 was not distributed with this source code in the
10*77c1e3ccSAndroid Build Coastguard Worker## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
11*77c1e3ccSAndroid Build Coastguard Worker##
12*77c1e3ccSAndroid Build Coastguard Workeruse strict;
13*77c1e3ccSAndroid Build Coastguard Workeruse warnings;
14*77c1e3ccSAndroid Build Coastguard Workeruse 5.010;
15*77c1e3ccSAndroid Build Coastguard Workeruse Getopt::Long;
16*77c1e3ccSAndroid Build Coastguard Worker
17*77c1e3ccSAndroid Build Coastguard Workermy $git_desc = '';
18*77c1e3ccSAndroid Build Coastguard Workermy $version_data;
19*77c1e3ccSAndroid Build Coastguard Workermy $version_filename;
20*77c1e3ccSAndroid Build Coastguard WorkerGetOptions('version_data=s' => \$version_data,
21*77c1e3ccSAndroid Build Coastguard Worker           'version_filename=s' => \$version_filename) or
22*77c1e3ccSAndroid Build Coastguard Worker  die("Invalid arg(s): $!");
23*77c1e3ccSAndroid Build Coastguard Worker
24*77c1e3ccSAndroid Build Coastguard Workerif (!defined $version_data || length($version_data) == 0 ||
25*77c1e3ccSAndroid Build Coastguard Worker    !defined $version_filename || length($version_filename) == 0) {
26*77c1e3ccSAndroid Build Coastguard Worker  die("--version_data and --version_filename are required.");
27*77c1e3ccSAndroid Build Coastguard Worker}
28*77c1e3ccSAndroid Build Coastguard Worker
29*77c1e3ccSAndroid Build Coastguard Worker# Determine if $version_data is a filename or a git tag/description.
30*77c1e3ccSAndroid Build Coastguard Workermy $version_string;
31*77c1e3ccSAndroid Build Coastguard Workerchomp($version_data);
32*77c1e3ccSAndroid Build Coastguard Workerif (-r $version_data) {
33*77c1e3ccSAndroid Build Coastguard Worker  # $version_data is the path to the CHANGELOG. Parse the most recent version.
34*77c1e3ccSAndroid Build Coastguard Worker  my $changelog_filename = $version_data;
35*77c1e3ccSAndroid Build Coastguard Worker  open(my $changelog_file, '<', $changelog_filename) or
36*77c1e3ccSAndroid Build Coastguard Worker    die("Unable to open CHANGELOG @ $changelog_filename: $!.");
37*77c1e3ccSAndroid Build Coastguard Worker
38*77c1e3ccSAndroid Build Coastguard Worker  while (my $line = <$changelog_file>) {
39*77c1e3ccSAndroid Build Coastguard Worker    my @split_line = split(" ", $line, 3);
40*77c1e3ccSAndroid Build Coastguard Worker    next if @split_line < 2;
41*77c1e3ccSAndroid Build Coastguard Worker    $version_string = $split_line[1];
42*77c1e3ccSAndroid Build Coastguard Worker    last if substr($version_string, 0, 1) eq "v";
43*77c1e3ccSAndroid Build Coastguard Worker  }
44*77c1e3ccSAndroid Build Coastguard Worker  close($changelog_file);
45*77c1e3ccSAndroid Build Coastguard Worker} else {
46*77c1e3ccSAndroid Build Coastguard Worker  # $version_data is either a tag name or a full git description, one of:
47*77c1e3ccSAndroid Build Coastguard Worker  # tagName OR tagName-commitsSinceTag-shortCommitHash
48*77c1e3ccSAndroid Build Coastguard Worker  # In either case we want the first element of the array returned by split.
49*77c1e3ccSAndroid Build Coastguard Worker  $version_string = (split("-", $version_data))[0];
50*77c1e3ccSAndroid Build Coastguard Worker  $git_desc = $version_data;
51*77c1e3ccSAndroid Build Coastguard Worker}
52*77c1e3ccSAndroid Build Coastguard Worker
53*77c1e3ccSAndroid Build Coastguard Workerif (substr($version_string, 0, 1) eq "v") {
54*77c1e3ccSAndroid Build Coastguard Worker  $version_string = substr($version_string, 1);
55*77c1e3ccSAndroid Build Coastguard Worker}
56*77c1e3ccSAndroid Build Coastguard Worker
57*77c1e3ccSAndroid Build Coastguard Workermy @version_components = split('\.', $version_string, 4);
58*77c1e3ccSAndroid Build Coastguard Workermy $version_major = $version_components[0];
59*77c1e3ccSAndroid Build Coastguard Workermy $version_minor = $version_components[1];
60*77c1e3ccSAndroid Build Coastguard Workermy $version_patch = $version_components[2];
61*77c1e3ccSAndroid Build Coastguard Worker
62*77c1e3ccSAndroid Build Coastguard Workermy $version_extra = "";
63*77c1e3ccSAndroid Build Coastguard Workerif (length($git_desc) > 0) {
64*77c1e3ccSAndroid Build Coastguard Worker  my @git_desc_components = split('-', $git_desc, 2);
65*77c1e3ccSAndroid Build Coastguard Worker  if (@git_desc_components > 1) {
66*77c1e3ccSAndroid Build Coastguard Worker    $version_extra = $git_desc_components[1];
67*77c1e3ccSAndroid Build Coastguard Worker  }
68*77c1e3ccSAndroid Build Coastguard Worker}
69*77c1e3ccSAndroid Build Coastguard Worker
70*77c1e3ccSAndroid Build Coastguard Workeropen(my $version_file, '>', $version_filename) or
71*77c1e3ccSAndroid Build Coastguard Worker  die("Cannot open $version_filename: $!");
72*77c1e3ccSAndroid Build Coastguard Worker
73*77c1e3ccSAndroid Build Coastguard Workermy $version_packed = "((VERSION_MAJOR << 16) | (VERSION_MINOR << 8) | (VERSION_PATCH))";
74*77c1e3ccSAndroid Build Coastguard Workermy $year = (localtime)[5] + 1900;
75*77c1e3ccSAndroid Build Coastguard Workermy $lic_block = << "EOF";
76*77c1e3ccSAndroid Build Coastguard Worker/*
77*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) $year, Alliance for Open Media. All rights reserved.
78*77c1e3ccSAndroid Build Coastguard Worker *
79*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
80*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
81*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
82*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
83*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
84*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
85*77c1e3ccSAndroid Build Coastguard Worker */
86*77c1e3ccSAndroid Build Coastguard WorkerEOF
87*77c1e3ccSAndroid Build Coastguard Worker
88*77c1e3ccSAndroid Build Coastguard Workerselect $version_file;
89*77c1e3ccSAndroid Build Coastguard Workerif (length($git_desc)) {
90*77c1e3ccSAndroid Build Coastguard Worker  print << "EOF";
91*77c1e3ccSAndroid Build Coastguard Worker$lic_block
92*77c1e3ccSAndroid Build Coastguard Worker#ifndef AOM_VERSION_H_
93*77c1e3ccSAndroid Build Coastguard Worker#define AOM_VERSION_H_
94*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_MAJOR $version_major
95*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_MINOR $version_minor
96*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_PATCH $version_patch
97*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_EXTRA \"$version_extra\"
98*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_PACKED \\
99*77c1e3ccSAndroid Build Coastguard Worker  $version_packed
100*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_STRING_NOSP \"$git_desc\"
101*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_STRING \" $git_desc\"
102*77c1e3ccSAndroid Build Coastguard Worker#endif  // AOM_VERSION_H_
103*77c1e3ccSAndroid Build Coastguard WorkerEOF
104*77c1e3ccSAndroid Build Coastguard Worker} else {
105*77c1e3ccSAndroid Build Coastguard Worker  print << "EOF";
106*77c1e3ccSAndroid Build Coastguard Worker$lic_block
107*77c1e3ccSAndroid Build Coastguard Worker#ifndef AOM_VERSION_H_
108*77c1e3ccSAndroid Build Coastguard Worker#define AOM_VERSION_H_
109*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_MAJOR $version_major
110*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_MINOR $version_minor
111*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_PATCH $version_patch
112*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_EXTRA \"$version_extra\"
113*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_PACKED \\
114*77c1e3ccSAndroid Build Coastguard Worker  $version_packed
115*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_STRING_NOSP \"v$version_string\"
116*77c1e3ccSAndroid Build Coastguard Worker#define VERSION_STRING \" v$version_string\"
117*77c1e3ccSAndroid Build Coastguard Worker#endif  // AOM_VERSION_H_
118*77c1e3ccSAndroid Build Coastguard WorkerEOF
119*77c1e3ccSAndroid Build Coastguard Worker}
120*77c1e3ccSAndroid Build Coastguard Workerclose($version_file);
121