aboutsummaryrefslogtreecommitdiff
path: root/gcc/pex.h
blob: 9c6816a38fd1ccf10885c8836f7e118ca4fef0ea (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* C++ wrapper around libiberty's pex API.
   Copyright (C) 2025 Free Software Foundation, Inc.
   Contributed by David Malcolm <dmalcolm@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifndef GCC_PEX_H
#define GCC_PEX_H

struct file_wrapper
{
  enum class ownership { owned, borrowed };

  file_wrapper (FILE *file, enum ownership ownership)
  : m_file (file),
    m_ownership (ownership)
  {
  }
  ~file_wrapper ()
  {
    if (m_ownership == ownership::owned)
      {
	gcc_assert (m_file);
	fclose (m_file);
      }
  }

  std::unique_ptr<std::vector<char>>
  read_all ();

  FILE *m_file;
  enum ownership m_ownership;
};

// RAII wrapper around pex_obj

struct pex
{
  pex (int flags, const char *pname, const char *tempbase)
  : m_obj (pex_init (flags, pname, tempbase))
  {
  }

  ~pex ()
  {
    pex_free (m_obj);
  }

  const char *
  run (int flags, const char *executable, char * const *argv,
       const char *outname, const char *errname, int *err)
  {
    return pex_run (m_obj, flags, executable, argv, outname, errname, err);
  }

  const char *
  run (int flags, const char *executable, const std::vector<std::string> &args,
       const char *outname, const char *errname, int *err);

  file_wrapper
  input_file (int flags, const char *in_name)
  {
    return file_wrapper (pex_input_file (m_obj, flags, in_name),
			 /* closed on first call to pex_run.  */
			 file_wrapper::ownership::borrowed);
  }

  file_wrapper
  input_pipe (bool binary = true)
  {
    return file_wrapper (pex_input_pipe (m_obj, binary),
			 /* closed on first call to pex_run.  */
			 file_wrapper::ownership::borrowed);
  }

  file_wrapper
  read_output (bool binary = true)
  {
    return file_wrapper (pex_read_output (m_obj, binary),
			 file_wrapper::ownership::borrowed);
  }

  pex_obj *m_obj;
};

#endif /* GCC_PEX_H */