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