blob: eca09dd75362582f9798db17aa0b85c4d8574029 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// Copyright 2015 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017#include "file.h"
18
19#include <fcntl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090024#include "log.h"
25#include "parser.h"
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090026#include "stmt.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090027
28Makefile::Makefile(const string& filename)
Shinichiro Hamaji07550472016-02-29 19:56:50 +090029 : mtime_(0), filename_(filename), exists_(false) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090030 int fd = open(filename.c_str(), O_RDONLY);
31 if (fd < 0) {
32 return;
33 }
34
35 struct stat st;
36 if (fstat(fd, &st) < 0) {
37 PERROR("fstat failed for %s", filename.c_str());
38 }
39
Shinichiro Hamaji07550472016-02-29 19:56:50 +090040 size_t len = st.st_size;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090041 mtime_ = st.st_mtime;
Shinichiro Hamaji07550472016-02-29 19:56:50 +090042 buf_.resize(len);
43 exists_ = true;
44 ssize_t r = read(fd, &buf_[0], len);
45 if (r != static_cast<ssize_t>(len)) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090046 if (r < 0)
47 PERROR("read failed for %s", filename.c_str());
Shinichiro Hamaji07550472016-02-29 19:56:50 +090048 ERROR("Unexpected read length=%zd expected=%zu", r, len);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090049 }
50
51 if (close(fd) < 0) {
52 PERROR("close failed for %s", filename.c_str());
53 }
54
55 Parse(this);
56}
57
58Makefile::~Makefile() {
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090059 for (Stmt* stmt : stmts_)
60 delete stmt;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090061}