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# Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
26#
27
28#
29# Check that link-editor mapfiles contain a valid mapfile header block
30#
31
32MAPFILE = '''
33WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
34Object versioning must comply with the rules detailed in
35
36	usr/src/lib/README.mapfiles
37
38You should not be making modifications here until you've read the most current
39copy of that file. If you need help, contact a gatekeeper for guidance.
40'''
41
42import re, sys
43from onbld.Checks import CmtBlk
44
45MAPFILE = MAPFILE.splitlines()[1:]		# Don't include initial \n
46
47def mapfilechk(fh, filename=None, verbose=False, output=sys.stderr):
48	if filename:
49		name = filename
50	else:
51		name = fh.name
52
53	# Verify that the mapfile is using version 2 syntax. Read and discard
54	# comment and empty lines until the first non-empty line is seen.
55	# This line must be '$mapfile_version 2'.
56	CmtRE = re.compile(r'#.*$')
57	LeadingWSRE = re.compile(r'^\s+')
58	VersionRE = re.compile(r'^\$mapfile_version\s+2\s*$')
59	for line in fh:
60		line = CmtRE.sub(r'', line)
61		line = LeadingWSRE.sub(r'', line)
62		if line == '' :
63			continue
64
65		# First non=empty line must be version declaration
66		if not VersionRE.match(line):
67			output.write("Warning: mapfile version 2 syntax"
68				" expected in file %s\n" % name)
69			return 1
70
71		# We have verified version 2 syntax. Exit the loop
72		break
73
74	# If the mapfile contains a SYMBOL_VERSION directive, the file
75	# must include a copy of the MAPFILE warning comment above. The
76	# comment is specific to symbol versioning, so we don't harrass
77	# the authors of mapfiles used exclusively for other purposes.
78	SymVerRE = re.compile(r'^\s*symbol_version\s+', re.IGNORECASE)
79	for line in fh:
80		# If we find a SYMBOL_VERSION, then verify that the comment
81		# is present. The comment usually precedes the mapfile_version
82		# comment and any mapfile directives (including SYMBOL_VERSION),
83		# so we need to rewind the file. This is more efficient than it
84		# might seem: All of these items are near the top of the file,
85		# so not not many lines are read, and file contents are
86		# bufferred.
87		if SymVerRE.match(line):
88			fh.seek(0);
89			return CmtBlk.cmtblkchk(fh, 'MAPFILE', MAPFILE,
90				filename=filename, verbose=verbose,
91				output=output)
92
93	# Comment is not required.
94	return 0
95