1bfed486aSAli Bahrami#! /usr/bin/python
2bfed486aSAli Bahrami#
3bfed486aSAli Bahrami# CDDL HEADER START
4bfed486aSAli Bahrami#
5bfed486aSAli Bahrami# The contents of this file are subject to the terms of the
6bfed486aSAli Bahrami# Common Development and Distribution License (the "License").
7bfed486aSAli Bahrami# You may not use this file except in compliance with the License.
8bfed486aSAli Bahrami#
9bfed486aSAli Bahrami# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10bfed486aSAli Bahrami# or http://www.opensolaris.org/os/licensing.
11bfed486aSAli Bahrami# See the License for the specific language governing permissions
12bfed486aSAli Bahrami# and limitations under the License.
13bfed486aSAli Bahrami#
14bfed486aSAli Bahrami# When distributing Covered Code, include this CDDL HEADER in each
15bfed486aSAli Bahrami# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16bfed486aSAli Bahrami# If applicable, add the following below this CDDL HEADER, with the
17bfed486aSAli Bahrami# fields enclosed by brackets "[]" replaced with your own identifying
18bfed486aSAli Bahrami# information: Portions Copyright [yyyy] [name of copyright owner]
19bfed486aSAli Bahrami#
20bfed486aSAli Bahrami# CDDL HEADER END
21bfed486aSAli Bahrami#
22bfed486aSAli Bahrami
23bfed486aSAli Bahrami#
24cd3e9333SAli Bahrami# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25*ca13eaa5SAndy Fiddaman# Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
26bfed486aSAli Bahrami#
27bfed486aSAli Bahrami
28bfed486aSAli Bahrami#
29bfed486aSAli Bahrami# Check that link-editor mapfiles contain a valid mapfile header block
30bfed486aSAli Bahrami#
31bfed486aSAli Bahrami
32bfed486aSAli BahramiMAPFILE = '''
33bfed486aSAli BahramiWARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
34bfed486aSAli BahramiObject versioning must comply with the rules detailed in
35bfed486aSAli Bahrami
36bfed486aSAli Bahrami	usr/src/lib/README.mapfiles
37bfed486aSAli Bahrami
38bfed486aSAli BahramiYou should not be making modifications here until you've read the most current
39bfed486aSAli Bahramicopy of that file. If you need help, contact a gatekeeper for guidance.
40bfed486aSAli Bahrami'''
41bfed486aSAli Bahrami
42*ca13eaa5SAndy Fiddamanimport re, sys
43*ca13eaa5SAndy Fiddamanfrom onbld.Checks import CmtBlk
44bfed486aSAli Bahrami
45bfed486aSAli BahramiMAPFILE = MAPFILE.splitlines()[1:]		# Don't include initial \n
46bfed486aSAli Bahrami
47bfed486aSAli Bahramidef mapfilechk(fh, filename=None, verbose=False, output=sys.stderr):
48cd3e9333SAli Bahrami	if filename:
49cd3e9333SAli Bahrami		name = filename
50cd3e9333SAli Bahrami	else:
51cd3e9333SAli Bahrami		name = fh.name
52cd3e9333SAli Bahrami
53cd3e9333SAli Bahrami	# Verify that the mapfile is using version 2 syntax. Read and discard
54cd3e9333SAli Bahrami	# comment and empty lines until the first non-empty line is seen.
55cd3e9333SAli Bahrami	# This line must be '$mapfile_version 2'.
56*ca13eaa5SAndy Fiddaman	CmtRE = re.compile(r'#.*$')
57*ca13eaa5SAndy Fiddaman	LeadingWSRE = re.compile(r'^\s+')
58*ca13eaa5SAndy Fiddaman	VersionRE = re.compile(r'^\$mapfile_version\s+2\s*$')
59cd3e9333SAli Bahrami	for line in fh:
60cd3e9333SAli Bahrami		line = CmtRE.sub(r'', line)
61cd3e9333SAli Bahrami		line = LeadingWSRE.sub(r'', line)
62cd3e9333SAli Bahrami		if line == '' :
63cd3e9333SAli Bahrami			continue
64cd3e9333SAli Bahrami
65cd3e9333SAli Bahrami		# First non=empty line must be version declaration
66cd3e9333SAli Bahrami		if not VersionRE.match(line):
67cd3e9333SAli Bahrami			output.write("Warning: mapfile version 2 syntax"
68cd3e9333SAli Bahrami				" expected in file %s\n" % name)
69cd3e9333SAli Bahrami			return 1
70cd3e9333SAli Bahrami
71cd3e9333SAli Bahrami		# We have verified version 2 syntax. Exit the loop
72cd3e9333SAli Bahrami		break
73cd3e9333SAli Bahrami
74cd3e9333SAli Bahrami	# If the mapfile contains a SYMBOL_VERSION directive, the file
75cd3e9333SAli Bahrami	# must include a copy of the MAPFILE warning comment above. The
76cd3e9333SAli Bahrami	# comment is specific to symbol versioning, so we don't harrass
77cd3e9333SAli Bahrami	# the authors of mapfiles used exclusively for other purposes.
78*ca13eaa5SAndy Fiddaman	SymVerRE = re.compile(r'^\s*symbol_version\s+', re.IGNORECASE)
79cd3e9333SAli Bahrami	for line in fh:
80cd3e9333SAli Bahrami		# If we find a SYMBOL_VERSION, then verify that the comment
81cd3e9333SAli Bahrami		# is present. The comment usually precedes the mapfile_version
82cd3e9333SAli Bahrami		# comment and any mapfile directives (including SYMBOL_VERSION),
83cd3e9333SAli Bahrami		# so we need to rewind the file. This is more efficient than it
84cd3e9333SAli Bahrami		# might seem: All of these items are near the top of the file,
85cd3e9333SAli Bahrami		# so not not many lines are read, and file contents are
86cd3e9333SAli Bahrami		# bufferred.
87cd3e9333SAli Bahrami		if SymVerRE.match(line):
88cd3e9333SAli Bahrami			fh.seek(0);
89cd3e9333SAli Bahrami			return CmtBlk.cmtblkchk(fh, 'MAPFILE', MAPFILE,
90cd3e9333SAli Bahrami				filename=filename, verbose=verbose,
91cd3e9333SAli Bahrami				output=output)
92cd3e9333SAli Bahrami
93cd3e9333SAli Bahrami	# Comment is not required.
94cd3e9333SAli Bahrami	return 0
95