Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pre_build_script.py Source File

pre_build_script.py

00001 #
00002 # DAPLink Interface Firmware
00003 # Copyright (c) 2009-2019, ARM Limited, All Rights Reserved
00004 # SPDX-License-Identifier: Apache-2.0
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00007 # not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 # http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00014 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 #
00018 
00019 from __future__ import absolute_import
00020 from __future__ import print_function
00021 
00022 import sys
00023 import os
00024 import argparse
00025 from subprocess import check_output, CalledProcessError
00026 
00027 VERSION_GIT_FILE_TEMPLATE = """
00028 /**
00029  * @file    version_git.h
00030  * @brief   GIT version info
00031  *
00032  * DAPLink Interface Firmware
00033  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00034  * SPDX-License-Identifier: Apache-2.0
00035  *
00036  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00037  * not use this file except in compliance with the License.
00038  * You may obtain a copy of the License at
00039  *
00040  * http://www.apache.org/licenses/LICENSE-2.0
00041  *
00042  * Unless required by applicable law or agreed to in writing, software
00043  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00044  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00045  * See the License for the specific language governing permissions and
00046  * limitations under the License.
00047  */
00048 #ifndef VERSION_GIT_H
00049 #define VERSION_GIT_H
00050 
00051 #define GIT_COMMIT_SHA  \"%s\"
00052 #define GIT_LOCAL_MODS  %d
00053 
00054 #endif
00055 
00056 """
00057 
00058 
00059 
00060 def generate_version_file(version_git_dir):
00061 
00062     output_file = os.path.join(os.path.normpath(version_git_dir),'version_git.h')
00063     print("#> Pre-build script start")
00064 
00065     # Get the git SHA.
00066     print("#> Getting git SHA")
00067     try:
00068         git_sha = check_output("git rev-parse --verify HEAD", shell=True)
00069         git_sha = git_sha.decode().strip()
00070     except:
00071         print("#> ERROR: Failed to get git SHA, do you have git.exe in your PATH environment variable?")
00072         return 1
00073 
00074     # Check are there any local, uncommitted modifications.
00075     print("#> Checking for local changes")
00076     try:
00077         check_output("git diff --no-ext-diff --quiet --exit-code", shell=True)
00078     except (CalledProcessError, OSError):
00079         git_has_changes = 1
00080     else:
00081         git_has_changes = 0
00082 
00083 
00084     # Create the version file. Only overwrite an existing file if it changes.
00085     version_text = VERSION_GIT_FILE_TEMPLATE % (git_sha, git_has_changes)
00086     try:
00087         with open(output_file, 'r') as version_file:
00088             current_version_text = version_file.read()
00089     except IOError:
00090         current_version_text = ''
00091     if version_text != current_version_text:
00092         print("#> Writing git version file")
00093         with open(output_file, 'w+') as version_file:
00094             version_file.write(version_text)
00095     else:
00096         print("#> Keeping git version file since it didn't need to change")
00097 
00098     print("#> Pre-build script completed written %s" % output_file )
00099 
00100     return 0
00101 
00102 if __name__ == "__main__":
00103     parser = argparse.ArgumentParser(description='git version generator')
00104     parser.add_argument("--version_git_dir", type=str, default='../../../source/daplink/', help="directory to output version_git.h file")
00105     args = parser.parse_args()
00106     exit(generate_version_file(args.version_git_dir))