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