shell.h 2.2 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <fcntl.h>
#include <sys/stat.h>
D
dongdaxiang 已提交
19 20 21
#ifdef _WIN32
#include <windows.h>
#else
D
dongdaxiang 已提交
22
#include <sys/syscall.h>
D
dongdaxiang 已提交
23
#endif
D
dongdaxiang 已提交
24
#include <sys/types.h>
25
#ifndef _WIN32
D
dongdaxiang 已提交
26
#include <sys/wait.h>
27
#endif
D
dongdaxiang 已提交
28 29 30
#include <memory>
#include <string>
#include <utility>
D
dongdaxiang 已提交
31
#include "paddle/fluid/platform/port.h"
32
#include "paddle/fluid/string/string_helper.h"
D
dongdaxiang 已提交
33

34 35 36 37 38
#if defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON) || \
    defined(__ARM_NEON__)
#define PADDLE_ARM
#endif

D
dongdaxiang 已提交
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
namespace paddle {
namespace framework {

inline bool& shell_verbose_internal() {
  static bool x = false;
  return x;
}

inline bool shell_verbose() { return shell_verbose_internal(); }

inline void shell_set_verbose(bool x) { shell_verbose_internal() = x; }

extern std::shared_ptr<FILE> shell_fopen(const std::string& path,
                                         const std::string& mode);

extern std::shared_ptr<FILE> shell_popen(const std::string& cmd,
                                         const std::string& mode, int* err_no);

extern std::pair<std::shared_ptr<FILE>, std::shared_ptr<FILE>> shell_p2open(
    const std::string& cmd);

inline void shell_execute(const std::string& cmd) {
  int err_no = 0;
  do {
    err_no = 0;
    shell_popen(cmd, "w", &err_no);
  } while (err_no == -1);
}

68 69 70 71 72 73
// timeout:ms, default -1 means forever.
// sleep_inter:ms, default -1 means not sleep.
extern std::string shell_get_command_output(const std::string& cmd,
                                            int time_out = -1,
                                            int sleep_inter = -1,
                                            bool print_cmd = false);
D
dongdaxiang 已提交
74 75 76

}  // namespace framework
}  // namespace paddle