xref: /onnv-gate/usr/src/tools/onbld/Checks/Mapfile.py (revision 12692:4341b447c069)
18744SAli.Bahrami@Sun.COM#! /usr/bin/python
28744SAli.Bahrami@Sun.COM#
38744SAli.Bahrami@Sun.COM# CDDL HEADER START
48744SAli.Bahrami@Sun.COM#
58744SAli.Bahrami@Sun.COM# The contents of this file are subject to the terms of the
68744SAli.Bahrami@Sun.COM# Common Development and Distribution License (the "License").
78744SAli.Bahrami@Sun.COM# You may not use this file except in compliance with the License.
88744SAli.Bahrami@Sun.COM#
98744SAli.Bahrami@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
108744SAli.Bahrami@Sun.COM# or http://www.opensolaris.org/os/licensing.
118744SAli.Bahrami@Sun.COM# See the License for the specific language governing permissions
128744SAli.Bahrami@Sun.COM# and limitations under the License.
138744SAli.Bahrami@Sun.COM#
148744SAli.Bahrami@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
158744SAli.Bahrami@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
168744SAli.Bahrami@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
178744SAli.Bahrami@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
188744SAli.Bahrami@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
198744SAli.Bahrami@Sun.COM#
208744SAli.Bahrami@Sun.COM# CDDL HEADER END
218744SAli.Bahrami@Sun.COM#
228744SAli.Bahrami@Sun.COM
238744SAli.Bahrami@Sun.COM#
24*12692SAli.Bahrami@Oracle.COM# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
258744SAli.Bahrami@Sun.COM#
268744SAli.Bahrami@Sun.COM
278744SAli.Bahrami@Sun.COM#
288744SAli.Bahrami@Sun.COM# Check that link-editor mapfiles contain a valid mapfile header block
298744SAli.Bahrami@Sun.COM#
308744SAli.Bahrami@Sun.COM
318744SAli.Bahrami@Sun.COMMAPFILE = '''
328744SAli.Bahrami@Sun.COMWARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
338744SAli.Bahrami@Sun.COMObject versioning must comply with the rules detailed in
348744SAli.Bahrami@Sun.COM
358744SAli.Bahrami@Sun.COM	usr/src/lib/README.mapfiles
368744SAli.Bahrami@Sun.COM
378744SAli.Bahrami@Sun.COMYou should not be making modifications here until you've read the most current
388744SAli.Bahrami@Sun.COMcopy of that file. If you need help, contact a gatekeeper for guidance.
398744SAli.Bahrami@Sun.COM'''
408744SAli.Bahrami@Sun.COM
41*12692SAli.Bahrami@Oracle.COMimport re, sys, CmtBlk
428744SAli.Bahrami@Sun.COM
438744SAli.Bahrami@Sun.COMMAPFILE = MAPFILE.splitlines()[1:]		# Don't include initial \n
448744SAli.Bahrami@Sun.COM
458744SAli.Bahrami@Sun.COMdef mapfilechk(fh, filename=None, verbose=False, output=sys.stderr):
46*12692SAli.Bahrami@Oracle.COM	if filename:
47*12692SAli.Bahrami@Oracle.COM		name = filename
48*12692SAli.Bahrami@Oracle.COM	else:
49*12692SAli.Bahrami@Oracle.COM		name = fh.name
50*12692SAli.Bahrami@Oracle.COM
51*12692SAli.Bahrami@Oracle.COM	# Verify that the mapfile is using version 2 syntax. Read and discard
52*12692SAli.Bahrami@Oracle.COM	# comment and empty lines until the first non-empty line is seen.
53*12692SAli.Bahrami@Oracle.COM	# This line must be '$mapfile_version 2'.
54*12692SAli.Bahrami@Oracle.COM        CmtRE = re.compile(r'#.*$')
55*12692SAli.Bahrami@Oracle.COM        LeadingWSRE = re.compile(r'^\s+')
56*12692SAli.Bahrami@Oracle.COM        VersionRE = re.compile(r'^\$mapfile_version\s+2\s*$')
57*12692SAli.Bahrami@Oracle.COM	for line in fh:
58*12692SAli.Bahrami@Oracle.COM		line = CmtRE.sub(r'', line)
59*12692SAli.Bahrami@Oracle.COM		line = LeadingWSRE.sub(r'', line)
60*12692SAli.Bahrami@Oracle.COM		if line == '' :
61*12692SAli.Bahrami@Oracle.COM			continue
62*12692SAli.Bahrami@Oracle.COM
63*12692SAli.Bahrami@Oracle.COM		# First non=empty line must be version declaration
64*12692SAli.Bahrami@Oracle.COM		if not VersionRE.match(line):
65*12692SAli.Bahrami@Oracle.COM			output.write("Warning: mapfile version 2 syntax"
66*12692SAli.Bahrami@Oracle.COM				" expected in file %s\n" % name)
67*12692SAli.Bahrami@Oracle.COM			return 1
68*12692SAli.Bahrami@Oracle.COM
69*12692SAli.Bahrami@Oracle.COM		# We have verified version 2 syntax. Exit the loop
70*12692SAli.Bahrami@Oracle.COM		break
71*12692SAli.Bahrami@Oracle.COM
72*12692SAli.Bahrami@Oracle.COM
73*12692SAli.Bahrami@Oracle.COM	# If the mapfile contains a SYMBOL_VERSION directive, the file
74*12692SAli.Bahrami@Oracle.COM	# must include a copy of the MAPFILE warning comment above. The
75*12692SAli.Bahrami@Oracle.COM	# comment is specific to symbol versioning, so we don't harrass
76*12692SAli.Bahrami@Oracle.COM	# the authors of mapfiles used exclusively for other purposes.
77*12692SAli.Bahrami@Oracle.COM        SymVerRE = re.compile(r'^\s*symbol_version\s+', re.IGNORECASE)
78*12692SAli.Bahrami@Oracle.COM	for line in fh:
79*12692SAli.Bahrami@Oracle.COM		# If we find a SYMBOL_VERSION, then verify that the comment
80*12692SAli.Bahrami@Oracle.COM		# is present. The comment usually precedes the mapfile_version
81*12692SAli.Bahrami@Oracle.COM		# comment and any mapfile directives (including SYMBOL_VERSION),
82*12692SAli.Bahrami@Oracle.COM		# so we need to rewind the file. This is more efficient than it
83*12692SAli.Bahrami@Oracle.COM		# might seem: All of these items are near the top of the file,
84*12692SAli.Bahrami@Oracle.COM		# so not not many lines are read, and file contents are
85*12692SAli.Bahrami@Oracle.COM		# bufferred.
86*12692SAli.Bahrami@Oracle.COM		if SymVerRE.match(line):
87*12692SAli.Bahrami@Oracle.COM			fh.seek(0);
88*12692SAli.Bahrami@Oracle.COM			return CmtBlk.cmtblkchk(fh, 'MAPFILE', MAPFILE,
89*12692SAli.Bahrami@Oracle.COM				filename=filename, verbose=verbose,
90*12692SAli.Bahrami@Oracle.COM				output=output)
91*12692SAli.Bahrami@Oracle.COM
92*12692SAli.Bahrami@Oracle.COM	# Comment is not required.
93*12692SAli.Bahrami@Oracle.COM	return 0
94