ATLAS Offline Software
cmake_newpkg.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
3 # @file PyUtils.scripts.cmake_newpkg
4 # @purpose streamline and ease the creation of new cmake packages
5 # @author Will Buttinger
6 # @date Feb 2017
7 
8 __version__ = "$Revision: 795362 $"
9 __author__ = "Will buttinger"
10 __doc__ = "streamline and ease the creation of new cmake packages"
11 
12 
13 import os
14 import textwrap
15 import subprocess
16 import PyUtils.acmdlib as acmdlib
17 
18 
19 @acmdlib.command(
20  name='cmake.new-pkg'
21  )
22 @acmdlib.argument(
23  'pkgname',
24  help="(fully qualified) name of the new package"
25  )
26 @acmdlib.argument(
27  '--author',
28  default='${USER}',
29  help='name of the author of this new package'
30  )
31 
32 def main(args):
33  """create a new cmake package with sensible atlas-oriented defaults
34 
35  ex:
36  $ acmd cmake new-pkg Control/MyContainer/NewPackage
37  """
38  sc = 0
39 
40  full_pkg_name = args.pkgname
41  if full_pkg_name[0] == '/':
42  full_pkg_name = full_pkg_name[1:]
43 
44  pkg_path = os.path.dirname(os.getcwd() + "/" + full_pkg_name)
45  pkg_name = os.path.basename(full_pkg_name)
46  pkg_vers = '%s-00-00-00' % pkg_name
47  author = os.path.expanduser(os.path.expandvars(args.author))
48 
49  if os.path.exists(pkg_path+"/"+pkg_name):
50  print("ERROR: %s package already exists" % full_pkg_name)
51  return 1
52 
53  print(textwrap.dedent("""\
54  ::: creating package [%(full_pkg_name)s]...
55  ::: - pkg name: %(pkg_name)s
56  ::: - pkg version: %(pkg_vers)s
57  ::: - pkg path: %(pkg_path)s
58  ::: - author: %(author)s""" % locals()))
59 
60 
61  #create the directories
62  try:
63  os.makedirs(pkg_path+"/"+pkg_name+"/"+pkg_name)
64  os.makedirs(pkg_path+"/"+pkg_name+"/src")
65  os.makedirs(pkg_path+"/"+pkg_name+"/share")
66  os.makedirs(pkg_path+"/"+pkg_name+"/python")
67  with open(pkg_path+"/"+pkg_name+"/python/__init__.py",'w') as f: pass
68  os.makedirs(pkg_path+"/"+pkg_name+"/data")
69  with open(pkg_path+"/"+pkg_name+"/data/ExampleData",'w') as f: f.write("this file will be accessible through PathResolverFindDataFile(\""+pkg_name+"/ExampleData\")")
70  os.makedirs(pkg_path+"/"+pkg_name+"/util")
71  os.makedirs(pkg_path+"/"+pkg_name+"/scripts")
72  except OSError:
73  print("ERROR while making directories for " % (pkg_path+"/"+pkg_name+"/src"))
74  return -1
75 
76 
77  with open(os.path.join(pkg_path+"/"+pkg_name,'CMakeLists.txt'), 'w') as req:
78  print(textwrap.dedent("""\
79  ## automatically generated CMakeLists.txt file
80 
81  # Declare the package
82  atlas_subdir( %(pkg_name)s )
83 
84  # Declare external dependencies ... default here is to include ROOT
85  find_package( ROOT COMPONENTS MathCore RIO Core Tree Hist )
86 
87  # Declare public and private dependencies
88  # Public dependencies are anything that appears in the headers in public include dir
89  # Private is anything else
90 
91  # An example is included
92  atlas_depends_on_subdirs(
93  PUBLIC
94 
95  PRIVATE
96  # Control/AthAnalysisBaseComps
97  )
98 
99  # Declare package as a library
100  # Note the convention that library names get "Lib" suffix
101  # Any package you add to dependencies above, you should add
102  # to LINK_LIBRARIES line below (see the example)
103  atlas_add_library( %(pkg_name)sLib src/*.cxx
104  PUBLIC_HEADERS %(pkg_name)s
105  INCLUDE_DIRS ${ROOT_INCLUDE_DIRS}
106  LINK_LIBRARIES ${ROOT_LIBRARIES}
107  #AthAnalysisBaseCompsLib
108  )
109 
110  # if you add components (tools, algorithms) to this package
111  # uncomment the next lines
112  # atlas_add_component( %(pkg_name)s src/components/*.cxx
113  # NOCLIDDB
114  # LINK_LIBRARIES %(pkg_name)sLib
115  # )
116 
117  # if you add an application (exe) to this package
118  # declare it like this (note convention that apps live in util dir)
119  # atlas_add_executable( MyApp util/myApp.cxx
120  # LINK_LIBRARIES %(pkg_name)sLib
121  # )
122 
123  # Install python modules, joboptions, and share content
124  atlas_install_python_modules( python/*.py )
125  atlas_install_joboptions( share/*.py )
126  atlas_install_data( data/* )
127  # You can access your data from code using path resolver, e.g.
128  # PathResolverFindCalibFile("%(pkg_name)s/file.txt")
129 
130 
131  """%locals()),file=req)
132 
133  #also create a version.cmake file with PackageName-00-00-01 in it
134  with open(os.path.join(pkg_path+"/"+pkg_name,'version.cmake'), 'w') as req:
135  print(("%s-00-00-01" % pkg_name),file=req)
136 
137  #need to reconfigure cmake so it knows about the new files
138  #rely on the WorkDir_DIR env var for this
139  workDir = os.environ.get("WorkDir_DIR")
140  if workDir is None:
141  print("::: ERROR No WorkDir_DIR env var, did you forget to source the setup.sh script?")
142  print( "::: ERROR Please do this and reconfigure cmake manually!")
143  else:
144  print( "::: INFO Reconfiguring cmake %s/../." % workDir)
145  res = subprocess.getstatusoutput('cmake %s/../.' % workDir)
146  if res[0]!=0:
147  print ("::: WARNING reconfigure unsuccessful. Please reconfigure manually!")
148 
149 
150  print ("::: creating package [%(full_pkg_name)s]... [done]" % locals())
151 
152 
153 
154  return sc
155 
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.scripts.cmake_newpkg.main
def main(args)
Definition: cmake_newpkg.py:32
Trk::open
@ open
Definition: BinningType.h:40