aboutsummaryrefslogtreecommitdiff
path: root/tools/clang_util.py
blob: 90632d5321d791211080e92b0239a7ddb03a579e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
# reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file

from __future__ import absolute_import
from __future__ import print_function
from exec_util import exec_cmd
import os
import sys

# Script directory.
script_dir = os.path.dirname(__file__)
root_dir = os.path.join(script_dir, os.pardir)

if sys.platform == 'win32':
  clang_format_exe = 'buildtools/win/clang-format.exe'
elif sys.platform == 'darwin':
  clang_format_exe = 'buildtools/mac/clang-format'
elif sys.platform.startswith('linux'):
  clang_format_exe = 'buildtools/linux64/clang-format'
else:
  raise Exception("Unsupported platform: %s" % sys.platform)


def clang_format(file_name, file_contents):
  # -assume-filename is necessary to find the .clang-format file and determine
  # the language when specifying contents via stdin.
  result = exec_cmd("%s -assume-filename=%s" %
                    (os.path.join(script_dir, clang_format_exe),
                     file_name), root_dir, file_contents.encode('utf-8'))
  if result['err'] != '':
    print("clang-format error: %s" % result['err'])
  if result['out'] != '':
    output = result['out']
    if sys.platform == 'win32':
      # Convert to Unix line endings.
      output = output.replace("\r", "")
    return output
  return None