]> jfr.im git - irc/evilnet/znc.git/blame - znc-buildmod.cmake.in
Merge pull request #1383 from Phansa/master
[irc/evilnet/znc.git] / znc-buildmod.cmake.in
CommitLineData
e95f0ea6 1#!/bin/sh
561a1805
AS
2#
3# Copyright (C) 2004-2016 ZNC, see the NOTICE file for details.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
e95f0ea6
AS
18# http://stackoverflow.com/questions/18993438/shebang-env-preferred-python-version
19# http://stackoverflow.com/questions/12070516/conditional-shebang-line-for-different-versions-of-python
20""":"
21which python3 >/dev/null 2>&1 && exec python3 "$0" "$@"
22which python >/dev/null 2>&1 && exec python "$0" "$@"
23which python2 >/dev/null 2>&1 && exec python2 "$0" "$@"
24echo "Error: znc-buildmod requires python"
25exec echo "Either install python, or use cmake directly"
26":"""
27
28from __future__ import print_function
561a1805
AS
29
30import argparse
31import glob
32import os
33import shutil
34import subprocess
e95f0ea6 35import sys
561a1805
AS
36import tempfile
37
e95f0ea6
AS
38if sys.version_info < (3, 0):
39 class TemporaryDirectory(object):
40 def __enter__(self):
41 self.name = tempfile.mkdtemp()
42 return self.name
43 def __exit__(self, *a, **k):
44 shutil.rmtree(self.name)
45 tempfile.TemporaryDirectory = TemporaryDirectory
46
561a1805
AS
47parser = argparse.ArgumentParser(
48 description='Build external ZNC modules and place the results to '
49 'current directory. Several modules can be built at once.',
50 epilog='Adjustable environment variables: CXXFLAGS, LDFLAGS, LIBS')
51parser.add_argument('-v', '--verbose', action='count', default=0,
52 help='use -vvv for more verbosity')
53parser.add_argument('files', nargs='+', metavar='file.cpp',
54 help="path to the module's source file")
55args = parser.parse_args()
56
57with tempfile.TemporaryDirectory() as cmdir:
58 with open(os.path.join(cmdir, 'CMakeLists.txt'), 'w') as cm:
82b30ced 59 print('cmake_minimum_required(VERSION 3.1)', file=cm)
561a1805 60 print('project(ExternalModules)', file=cm)
88d1e27c
AS
61 print('find_package(ZNC @ZNC_VERSION_MAJOR@.@ZNC_VERSION_MINOR@ HINTS '
62 '@CMAKE_INSTALL_FULL_DATADIR@/znc REQUIRED)', file=cm)
561a1805
AS
63 if args.verbose > 0:
64 print('set(CMAKE_VERBOSE_MAKEFILE true)', file=cm)
65
66 for mod_cpp in args.files:
67 mod, _ = os.path.splitext(os.path.basename(mod_cpp))
68 print(file=cm)
69 print('add_library(module_{} MODULE {})'.format(
70 mod, os.path.abspath(mod_cpp)), file=cm)
71 print('znc_setup_module(TARGET module_{} NAME {})'.format(mod, mod),
72 file=cm)
73 print('target_link_libraries(module_{} PRIVATE {})'.format(
74 mod, os.environ.get('LIBS', '')), file=cm)
75
76 if args.verbose > 0:
77 with open(os.path.join(cmdir, 'CMakeLists.txt')) as cm:
78 print(cm.read())
79
80 with tempfile.TemporaryDirectory() as build:
81 command = ['cmake', '-G@CMAKE_GENERATOR@',
82 '-DCMAKE_C_COMPILER=@CMAKE_C_COMPILER@',
83 '-DCMAKE_CXX_COMPILER=@CMAKE_CXX_COMPILER@',
84 '-DCMAKE_CXX_FLAGS={} @CMAKE_CXX_FLAGS@'.format(
85 os.environ.get('CXXFLAGS', '')),
86 '-DCMAKE_MODULE_LINKER_FLAGS={} @CMAKE_MODULE_LINKER_FLAGS@'.format(
87 os.environ.get('LDFLAGS', '')),
88 cmdir]
89 if args.verbose > 1:
90 cmd.append('--debug-output')
91 if args.verbose > 2:
92 cmd.append('--trace')
93 if args.verbose > 0:
94 print(command)
95 subprocess.check_call(command, cwd=build)
96 subprocess.check_call(['cmake', '--build', '.'], cwd=build)
97
98 for so in glob.iglob(os.path.join(build, '*.so')):
99 shutil.copy(so, os.getcwd())